IDENTITY & DIAGONAL MATRICES

Identity matrix in LaTeX: 2×2, 3×3 and diagonal examples

Examples checked with MathTexLab’s base and AMS math renderer · Display style

To write an identity matrix in LaTeX, enter ones on the main diagonal and zeros in every other cell. For a diagonal matrix, replace those ones with your chosen diagonal entries. Blank cells are not a substitute for zeros.

Write an explicit identity matrix

For three rows and columns, put a one in cells (1,1), (2,2) and (3,3), and zeros elsewhere. The subscript on the letter records the intended size. The example writes every entry so the source is easy to inspect.

Use pmatrix for round brackets and bmatrix for square brackets. Separate columns with & and rows with \\. Both environments are provided by amsmath in a full LaTeX document.

2×2 identity matrix with pmatrix
I_2=\begin{pmatrix}1&0\\0&1\end{pmatrix}
Open example in editor
Write identity, diagonal and rectangular matrices in LaTeX example
I_3=\begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix}
Open example in editor
4×4 identity matrix with bmatrix
I_4=\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&1\end{bmatrix}
Open example in editor

Change the diagonal values

To build a diagonal example, replace those three ones with values such as 2, 5 and 7. Keep the off-diagonal cells zero. Blank cells in the Matrix Generator also output zero, but writing zeros explicitly can make the grid easier to check before copying.

Diagonal matrix with entries 2, 5 and 7
D=\begin{bmatrix}2&0&0\\0&5&0\\0&0&7\end{bmatrix}
Open example in editor

Separate dimensions from notation

A matrix can be rectangular. Three rows and two columns describe a 3-by-2 shape; it is not a square identity matrix. Changing brackets does not change dimensions. Count the columns in each source row if you are checking copied TeX by hand.

Avoid implying an evaluated result

A generator preset writes a pattern into the grid; it does not test whether an arbitrary matrix is invertible or calculate its inverse. If you include a statement about multiplication or a determinant beside the matrix, verify that claim independently. For a large example, inspect the scrollable preview and export at a readable size rather than forcing every entry into a narrow paragraph.

Build these entries interactively in the LaTeX Matrix Generator. For delimiter choices, compare the matrix bracket examples.

A row in an n-column matrix needs n entries and n−1 ampersands. Keep explicit zeros off the diagonal, match the case of the environment name, and close it with the same name used after \begin.

Syntax reference

MathJax supported-command reference. The examples above are limited to the features described here; additional packages are not implied.