How to Write a Matrix in LaTeX

To write a matrix in LaTeX, load amsmath and use pmatrix for round brackets or bmatrix for square ones, in math mode. Separate the entries in a row with & and end each row with \\.

LaTeX
\[ A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \]
OutputA equals the 2 by 2 matrix 1, 2; 3, 4 in round brackets.

Needs \usepackage{amsmath} in the preamble.

Full document you can compile
example.tex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[ A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \]
\end{document}

Which bracket does each environment give?

The first letter sets the delimiter: matrix has none, pmatrix parentheses, bmatrix square brackets, Bmatrix braces, vmatrix single bars for a determinant and Vmatrix double bars for a norm.

LaTeX
\[
\begin{matrix} a & b \\ c & d \end{matrix} \quad
\begin{pmatrix} a & b \\ c & d \end{pmatrix} \quad
\begin{bmatrix} a & b \\ c & d \end{bmatrix} \quad
\begin{Bmatrix} a & b \\ c & d \end{Bmatrix} \quad
\begin{vmatrix} a & b \\ c & d \end{vmatrix} \quad
\begin{Vmatrix} a & b \\ c & d \end{Vmatrix}
\]
OutputThe same 2 by 2 matrix a, b; c, d with no brackets, parentheses, square brackets, braces, single bars and double bars.

Needs \usepackage{amsmath} in the preamble.

Full document you can compile
example.tex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\begin{matrix} a & b \\ c & d \end{matrix} \quad
\begin{pmatrix} a & b \\ c & d \end{pmatrix} \quad
\begin{bmatrix} a & b \\ c & d \end{bmatrix} \quad
\begin{Bmatrix} a & b \\ c & d \end{Bmatrix} \quad
\begin{vmatrix} a & b \\ c & d \end{vmatrix} \quad
\begin{Vmatrix} a & b \\ c & d \end{Vmatrix}
\]
\end{document}

How do you write a large matrix in LaTeX with dots?

Use \cdots for horizontal dots, \vdots for vertical dots and \ddots for diagonal dots, each in its own cell. Entries with two indices need braces: a_{mn}.

LaTeX
\[ A = \begin{bmatrix}
  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{bmatrix} \]
OutputA general m by n matrix with entries a 1 1 to a m n, with horizontal, vertical and diagonal dots.

Needs \usepackage{amsmath} in the preamble.

Full document you can compile
example.tex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[ A = \begin{bmatrix}
  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{bmatrix} \]
\end{document}

Why does a wide matrix give "Extra alignment tab"?

The amsmath matrix environments allow 10 columns by default. An 11th & stops the compile with ! Extra alignment tab has been changed to \cr. Raise the limit in the preamble with \setcounter{MaxMatrixCols}{20}.

How do you write an augmented matrix?

The matrix environments can't draw a vertical line, so use array with a | in the column list, and add the brackets with \left[ and \right].

LaTeX
\[ \left[\begin{array}{cc|c}
  1 & 2 & 5 \\
  3 & 4 & 6
\end{array}\right] \]
OutputAn augmented matrix 1, 2 | 5; 3, 4 | 6 with a vertical line before the last column.

Needs \usepackage{amsmath} in the preamble.

Full document you can compile
example.tex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[ \left[\begin{array}{cc|c}
  1 & 2 & 5 \\
  3 & 4 & 6
\end{array}\right] \]
\end{document}

How do you put a small matrix in a line of text?

smallmatrix sets the entries in a smaller size so the matrix doesn't spread the lines apart. It has no brackets of its own; add them with \left( and \right).

LaTeX
The vector $\left(\begin{smallmatrix} x \\ y \end{smallmatrix}\right)$ fits in a line of text,
and so does $I = \left(\begin{smallmatrix} 1 & 0 \\ 0 & 1 \end{smallmatrix}\right)$.
OutputA sentence containing a small column vector x, y and the 2 by 2 identity matrix inline.

Needs \usepackage{amsmath} in the preamble.

Full document you can compile
example.tex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
The vector $\left(\begin{smallmatrix} x \\ y \end{smallmatrix}\right)$ fits in a line of text,
and so does $I = \left(\begin{smallmatrix} 1 & 0 \\ 0 & 1 \end{smallmatrix}\right)$.
\end{document}

Is there an easier way to type a matrix?

The equation editor has matrix templates with a live preview. For a grid of data rather than math, use a table instead. The online LaTeX converter turns each matrix environment into MathML.

Text drafted with AI from the amsmath documentation; every example compiled with TeX Live 2026; checked by Cem Demir. See the editorial policy.