Matrices in LaTeX (pmatrix, bmatrix)
Load amsmath and use the pmatrix environment for a matrix in parentheses or bmatrix for square brackets. Inside, & separates the columns and \\ ends a row.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{amsmath}Matrix in parentheses or square brackets
The environments work only in math mode. You do not declare the number of columns; LaTeX counts the & signs. Every entry is centred in its column.
\[
A = \begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6
\end{pmatrix}
\qquad
B = \begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Determinants and other delimiters
vmatrix puts single vertical lines around the entries, Vmatrix double lines, Bmatrix braces, and matrix nothing.
\[
\det(A) = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bc
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Dots for a general matrix
\cdots prints three horizontal dots, \vdots three vertical dots and \ddots three diagonal dots.
\[
\begin{pmatrix}
a_{11} & \cdots & a_{1n} \\
\vdots & \ddots & \vdots \\
a_{m1} & \cdots & a_{mn}
\end{pmatrix}
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Small matrix in a line of text
smallmatrix sets the entries in a size that fits into a line of text. It has no delimiters of its own; add them with \bigl( and \bigr).
The matrix $\bigl(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\bigr)$
fits into a line of text.Augmented matrix with a vertical line
The amsmath matrices have no column specification, so a vertical line needs the array environment. {cc|c} means two centred columns, a line and one more centred column; \left( and \right) add parentheses of the right height.
\[
\left(\begin{array}{cc|c}
1 & 2 & 5 \\
3 & 4 & 6
\end{array}\right)
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Pitfalls
- A matrix environment accepts at most 10 columns. An eleventh column stops with Extra alignment tab has been changed to \cr; raise the limit in the preamble with
\setcounter{MaxMatrixCols}{12}. - For right-aligned entries, for example numbers with minus signs,
mathtoolshas starred versions with an alignment argument:\begin{pmatrix*}[r].
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
amsmath - Package
mathtools - How-to Vectors in LaTeX
- How-to Systems of equations in LaTeX
- Error Extra alignment tab has been changed to \cr