Matrices in LaTeX
amsmath gives you a matrix environment for each kind of bracket. You write the entries the same way in all of them: & between columns, \\ at the end of each row. This guide covers the brackets, determinants, large matrices, augmented matrices and blocks.
Pick the brackets
\usepackage{amsmath} % preamble
\[
\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}
\quad
\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}
\]| Environment | Brackets | Usual meaning |
|---|---|---|
matrix | None | A plain grid |
pmatrix | ( ) | Matrices and vectors |
bmatrix | [ ] | Matrices, common in engineering |
Bmatrix | { } | Rarely used |
vmatrix | | | | Determinant |
Vmatrix | ‖ ‖ | Norm |
Every matrix must be inside math: \[...\], equation or $...$. Pick parentheses or square brackets and use the same everywhere in your report.
Determinants
\[
\det(A) =
\begin{vmatrix} a & b \\ c & d \end{vmatrix}
= ad - bc
\]Large matrices with dots
\[
A =
\begin{pmatrix}
a_{11} & a_{12} & \cdots & a_{1n} \\
a_{21} & a_{22} & \cdots & a_{2n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{m1} & a_{m2} & \cdots & a_{mn}
\end{pmatrix}
\]\cdots runs along a row, \vdots down a column and \ddots along the diagonal. The same pattern gives the identity matrix, with 1 on the diagonal and 0 elsewhere.
More than ten columns
amsmath matrices allow ten columns by default. An eleventh stops the compile with "Extra alignment tab has been changed to \cr". Raise the limit in the preamble:
\setcounter{MaxMatrixCols}{20}Aligning negative numbers
Columns are centred, so a minus sign pushes its number out of line. The mathtools package adds starred environments that take an alignment:
\usepackage{mathtools} % preamble, loads amsmath too
\begin{pmatrix*}[r]
-1 & 3 \\
2 & -4
\end{pmatrix*}A small matrix in a sentence
The rotation $R = \left(\begin{smallmatrix} 0 & -1 \\ 1 & 0 \end{smallmatrix}\right)$ turns the plane by 90 degrees.smallmatrix has no brackets of its own, so add them with \left( and \right).
Augmented matrices
There is no ready-made environment, but array with a | in the column list draws the line:
\[
\left[\begin{array}{ccc|c}
1 & 2 & -1 & 3 \\
0 & 1 & 3 & 7 \\
0 & 0 & 1 & 2
\end{array}\right]
\]ccc|c means three centred columns, a line, then one more column. To show a row operation between two matrices, use \xrightarrow{R_1 \leftrightarrow R_2}.
Block matrices
\[
M =
\begin{pmatrix}
A & B \\
C & D
\end{pmatrix}
\qquad
\left[\begin{array}{cc|cc}
a_{11} & a_{12} & 0 & 0 \\
a_{21} & a_{22} & 0 & 0 \\
\hline
0 & 0 & b_{11} & b_{12} \\
0 & 0 & b_{21} & b_{22}
\end{array}\right]
\]Write the blocks as letters when the structure is the point. When the entries matter, draw the dividing lines with | in the column list and \hline between rows.
Operations
A^{\top} % transpose (or A^T)
A^{-1} % inverse
AB % product: just write them side by side
\[
\begin{pmatrix} a & b \\ c & d \end{pmatrix}
\begin{pmatrix} x \\ y \end{pmatrix}
=
\begin{pmatrix} ax + by \\ cx + dy \end{pmatrix}
\]A system of equations as a matrix
\[
\begin{cases}
2x + y = 8 \\
x + 3y = 13
\end{cases}
\iff
\begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix}
\begin{pmatrix} x \\ y \end{pmatrix}
=
\begin{pmatrix} 8 \\ 13 \end{pmatrix}
\]Labels on rows and columns
\[
\begin{array}{c|ccc}
& x_1 & x_2 & x_3 \\
\hline
y_1 & 1 & 2 & 3 \\
y_2 & 4 & 5 & 6
\end{array}
\]For labels around a bracketed matrix, the blkarray package does the layout, at the cost of a more verbose syntax.
Where to go next
- Writing math for align and cases
- Greek letters and symbols
- Tables, when your grid is data rather than math