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.

A=(123456)B=[abcd]A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} \qquad B = \begin{bmatrix} a & b \\ c & d \end{bmatrix}

Open this formula in the editor

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.

det⁡(A)=∣abcd∣=ad−bc\det(A) = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bc

Open this formula in the editor

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.

(a11⋯a1n⋮⋱⋮am1⋯amn)\begin{pmatrix} a_{11} & \cdots & a_{1n} \\ \vdots & \ddots & \vdots \\ a_{m1} & \cdots & a_{mn} \end{pmatrix}

Open this formula in the editor

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.
Result
The compiled example

Open in the editor

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.

(125346)\left(\begin{array}{cc|c} 1 & 2 & 5 \\ 3 & 4 & 6 \end{array}\right)

Open this formula in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages