To write a LaTeX matrix, load the amsmath package and use one of its matrix environments inside maths mode: pmatrix for round brackets, bmatrix for square brackets, or vmatrix for a determinant. Separate entries with & and end each row with \\. This guide shows all six environments side by side, then covers general m×n matrices with dots, inline matrices, augmented and block matrices, column alignment, spacing, the 10-column limit and the errors you will most likely see.
Every example on this page was compiled with Tectonic 0.17 (a self-contained TeX engine based on XeTeX) on 2 December 2026, and the error messages are copied from the real build logs. The code is standard LaTeX and works the same way in Overleaf. New to LaTeX? Start with our beginner’s LaTeX tutorial.
TL;DR: LaTeX Matrix Environments
| Environment | Brackets | Typical use |
|---|---|---|
matrix | None | Your own delimiters, or a grid of values |
pmatrix | ( ) | The usual choice for matrices and vectors |
bmatrix | [ ] | Matrices in many linear algebra texts |
Bmatrix | { } | Sets or grouped values |
vmatrix | | | | Determinants |
Vmatrix | ‖ ‖ | Matrix norms |
smallmatrix | None (add your own) | Matrices inside a line of text |
array | Your own, with \left/\right | Augmented and block matrices with lines |

How to Write a Matrix in LaTeX
All matrix environments come from amsmath, so put \usepackage{amsmath} in the preamble. The six environments take the same input and differ only in the brackets they draw:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\begin{matrix} 1 & 2 \\ 3 & 4 \end{matrix} \quad
\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \quad
\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \quad
\begin{Bmatrix} 1 & 2 \\ 3 & 4 \end{Bmatrix} \quad
\begin{vmatrix} 1 & 2 \\ 3 & 4 \end{vmatrix} \quad
\begin{Vmatrix} 1 & 2 \\ 3 & 4 \end{Vmatrix}
\]
\end{document}Inside each environment, & moves to the next column and \\ starts a new row. There is no \\ after the last row. Entries are centred in their columns by default.
Because the brackets are part of the environment, they scale to the height of the matrix automatically. You don’t need \left and \right as you would with array.
General m×n Matrix With Dots
For a general matrix, use \cdots (horizontal dots), \vdots (vertical) and \ddots (diagonal) as entries. Subscripts give the row and column index:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
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}
\]
\end{document}Use braces for multi-digit subscripts, such as a_{12}; a_12 would only lower the 1. More dot and accent commands are in our LaTeX symbols cheat sheet.
Common Matrix Examples: Identity, Transpose, Determinant and Products
These are the patterns that come up most in linear algebra notes. A product is just two matrices written next to each other; LaTeX adds no multiplication sign:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Identity and transpose:
\[
I_3 = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}
\qquad
A^{\mathsf{T}} = \begin{bmatrix} 1 & 3 \\ 2 & 4 \end{bmatrix}
\]
Determinant:
\[
\det A = \begin{vmatrix} 1 & 2 \\ 3 & 4 \end{vmatrix} = 1 \cdot 4 - 2 \cdot 3 = -2
\]
Matrix times vector:
\[
\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}
\begin{bmatrix} x \\ y \end{bmatrix}
=
\begin{bmatrix} x + 2y \\ 3x + 4y \end{bmatrix}
\]
\end{document}\mathsf{T} gives an upright sans-serif T for the transpose, which avoids confusion with a variable called T. Many authors use ^\top or ^T instead; pick one and keep it consistent. A system of equations in matrix form works the same way:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix}
\begin{pmatrix} x_1 \\ x_2 \end{pmatrix}
=
\begin{pmatrix} 5 \\ 10 \end{pmatrix}
\quad\Longrightarrow\quad
x_1 = 1,\; x_2 = 3
\]
\end{document}Inline Matrices With smallmatrix
A full pmatrix inside a paragraph is two lines tall and pushes the surrounding lines apart. smallmatrix uses a smaller font and tighter spacing so it fits in the line. It draws no brackets, so add them with \left( and \right):
\documentclass{article}
\usepackage{amsmath}
\begin{document}
A full matrix in a line $\begin{pmatrix} a & b \\ c & d \end{pmatrix}$ stretches the line spacing.
A small one $\left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right)$ fits the line.
\end{document}In the PDF, the line holding the full matrix grew to two text lines tall; the smallmatrix line kept its normal height. The mathtools package also offers psmallmatrix and bsmallmatrix with the brackets built in:
\documentclass{article}
\usepackage{mathtools}
\begin{document}
Inline $\begin{psmallmatrix} a & b \\ c & d \end{psmallmatrix}$ and $\begin{bsmallmatrix} a & b \\ c & d \end{bsmallmatrix}$.
\end{document}Augmented Matrices and Block Matrices With array
amsmath matrix environments can’t draw vertical lines. For an augmented matrix (a system of equations with the right-hand side separated by a bar), use array with a column spec, and wrap it in \left[ and \right]:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\left[
\begin{array}{ccc|c}
1 & 2 & -1 & 4 \\
0 & 1 & 3 & 5 \\
2 & 0 & 1 & 7
\end{array}
\right]
\]
\end{document}The spec {ccc|c} means three centred columns, a vertical line, then one more centred column. The same idea, plus \hline, gives a block matrix split into four parts:
\documentclass{article}
\usepackage{amsmath}
\usepackage{bm}
\begin{document}
Block matrix:
\[
M = \left[
\begin{array}{c|c}
A & B \\ \hline
C & D
\end{array}
\right]
\]
Bold notation: $\mathbf{A}\mathbf{x} = \mathbf{b}$ or $\bm{A}\bm{x} = \bm{b}$.
\end{document}The column spec works exactly like it does in tabular; our LaTeX tables guide explains the column letters and lines in more detail. For matrix names, \mathbf{A} gives upright bold, while \bm{A} from the bm package keeps the italic shape and is common for vectors and matrices in maths papers.
Align Matrix Columns Right or Left
Entries are centred, so a column with -1 and -40 looks ragged. The mathtools package (which loads amsmath for you) adds starred versions such as pmatrix* and bmatrix* that take an alignment option: [r], [l] or [c]:
\documentclass{article}
\usepackage{mathtools}
\begin{document}
Default (centred):
\[
\begin{pmatrix} -1 & 3 \\ 2 & -40 \end{pmatrix}
\]
Right-aligned with mathtools:
\[
\begin{pmatrix*}[r] -1 & 3 \\ 2 & -40 \end{pmatrix*}
\]
\end{document}In the default version, 3 sat in the middle above −40; with [r] the 3 lined up with the 0 of −40 and the 2 with the 1 of −1. Right alignment is usually the best choice for columns of numbers.
Adjust Row and Column Spacing
Fractions inside a matrix make rows look cramped. Increase \arraystretch to add space to every row, or put a length after \\ to add space after one row only:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Default:
\[
\begin{bmatrix} \frac{1}{2} & 0 \\ 0 & \frac{3}{4} \end{bmatrix}
\]
More room between rows:
\[
\renewcommand{\arraystretch}{1.5}
\begin{bmatrix} \frac{1}{2} & 0 \\ 0 & \frac{3}{4} \end{bmatrix}
\]
Extra space after one row only:
\[
\begin{bmatrix} \frac{1}{2} & 0 \\[4pt] 0 & \frac{3}{4} \end{bmatrix}
\]
\end{document}Putting \renewcommand inside the \[ ... \] keeps the change local to that equation. For column spacing, change \arraycolsep the same way, for example \setlength{\arraycolsep}{8pt}. To write the fractions themselves, see our LaTeX fractions guide.
Matrices With More Than 10 Columns
amsmath matrix environments allow at most 10 columns by default. A row with 12 entries fails:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\begin{bmatrix}
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12
\end{bmatrix}
\]
\end{document}Tectonic stopped with ! Extra alignment tab has been changed to \cr. at the eleventh &. The fix is to raise the MaxMatrixCols counter in the preamble:
\documentclass{article}
\usepackage{amsmath}
\setcounter{MaxMatrixCols}{20}
\begin{document}
\[
\begin{bmatrix}
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12
\end{bmatrix}
\]
\end{document}With the counter set to 20, the same 12-column matrix compiled on one line.
Common LaTeX Matrix Errors
| Error in the log | Cause | Fix |
|---|---|---|
Misplaced alignment tab character & | pmatrix (or another matrix environment) used without amsmath | Add \usepackage{amsmath} |
Extra alignment tab has been changed to \cr | More than 10 columns, or a row with more & than the array spec allows | \setcounter{MaxMatrixCols}{20}, or fix the array column spec |
Missing $ inserted | A matrix environment used outside maths mode (we got it at l.4 \begin{pmatrix}) | Wrap it in \[ ... \] or $ ... $ |
The first error is the confusing one, so here is the file that produced it:
\documentclass{article}
\begin{document}
\[
\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}
\]
\end{document}Without amsmath, Tectonic reported ! Misplaced alignment tab character &. at l.4 \begin{pmatrix} 1 &. The message points at the &, not at the missing package, because LaTeX’s core doesn’t define a pmatrix environment that would accept it. Overleaf’s default templates load amsmath, which is why the same code may work there and fail in a minimal local file.
Conclusion
For a LaTeX matrix, load amsmath and pick the environment by bracket: pmatrix for round, bmatrix for square, vmatrix for determinants. Use \cdots, \vdots and \ddots for general matrices, smallmatrix for inline ones, and array when you need lines for augmented or block matrices. mathtools adds column alignment, and MaxMatrixCols lifts the 10-column limit. If you write in Overleaf, our Overleaf guide shows how to set up a project with these packages.
How do you write a matrix in LaTeX?
Load amsmath with \usepackage{amsmath}, then write \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} inside maths mode. & separates columns and \\ ends a row.
What is the difference between pmatrix and bmatrix?
Only the brackets. pmatrix draws round parentheses and bmatrix draws square brackets. Bmatrix gives curly braces, vmatrix single bars and Vmatrix double bars.
How do I write a determinant in LaTeX?
Use the vmatrix environment, for example \det A = \begin{vmatrix} 1 & 2 \\ 3 & 4 \end{vmatrix}. It draws vertical bars that scale to the matrix height.
How do I draw a vertical line in a matrix?
amsmath matrix environments cannot draw lines. Use \left[ \begin{array}{cc|c} … \end{array} \right], where | in the column spec draws the line. This is the usual way to write an augmented matrix.
Why does my matrix with more than 10 columns fail?
amsmath allows 10 columns by default and reports Extra alignment tab has been changed to \cr. Add \setcounter{MaxMatrixCols}{20} (or any larger number) to the preamble.
How do I put a small matrix inside a sentence?
Use \left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right). smallmatrix uses a smaller font so the line spacing stays normal.







