How to write a matrix in LaTeX, with copyable examples
How to write matrices in LaTeX: the amsmath environments for round, square, curly and vertical brackets, augmented matrices, large m × n matrices with dots, block matrices, small inline matrices and determinants. Every example below shows the code with a copy button and the typeset result, and the generator builds the code for any size you need.
On this page
The short answer
Load amsmath in the preamble, then write the entries inside a matrix environment, with & between columns and \\ at the end of each row:
\usepackage{amsmath} % in the preamble
\[
A = \begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6
\end{pmatrix}
\]The environment name sets the brackets, and the matrix must sit inside maths mode: \[ … \] or equation for a displayed matrix, or $ … $ inline. The number of columns is worked out from the rows, so you never declare it.
Matrix generator
Choose the size and brackets, type or paste the entries, and copy the code. Paste a block of cells from Excel, Google Sheets or a CSV file into the first cell to fill the whole grid.
The six bracket styles
amsmath provides one environment per bracket style. They all take the same body.
| Environment | Brackets | Result | Typical use |
|---|---|---|---|
matrix | none | Inside your own delimiters | |
pmatrix | ( ) | Most textbooks and papers | |
bmatrix | [ ] | Engineering, many linear algebra courses | |
Bmatrix | { } | Rare; sets of vectors | |
vmatrix | | | | Determinants | |
Vmatrix | ‖ ‖ | Matrix norms |
Pick one style for a whole document. Round brackets are the most common in mathematics and computer science.
Column vectors and row vectors
A vector is just a matrix with one column or one row:
\mathbf{v} = \begin{pmatrix} x \\ y \\ z \end{pmatrix}, \qquad
\mathbf{w}^\top = \begin{pmatrix} w_1 & w_2 & w_3 \end{pmatrix}To save vertical space in running text, write a column vector as a transposed row: $(x, y, z)^\top$.
A general m × n matrix with dots
Use \cdots (horizontal), \vdots (vertical) and \ddots (diagonal) for the entries you leave out. Choose “General m × n with dots” in the generator to get this layout at any size.
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}Write a_{ij} with braces: a_ij puts only the i in the subscript. For two-digit or computed indices, add a comma: a_{i,j+1}.
Identity, zero and diagonal matrices
I_3 = \begin{pmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{pmatrix}, \qquad
D = \begin{pmatrix}
\lambda_1 & & \\
& \ddots & \\
& & \lambda_n
\end{pmatrix}Empty cells are allowed, which is the usual way to show that the off-diagonal entries of a diagonal matrix are zero. To make the zeros explicit but quiet, write a large 0 in one corner, or use \mathbf{0} for a zero block.
Augmented matrices
amsmath has no environment with a vertical bar, so use array with a | in the column specification and add brackets with \left[ … \right]. The {cc|c} here means two centred columns, a vertical rule, then one more column:
\left[\begin{array}{cc|c}
2 & 1 & 5 \\
1 & -3 & -1
\end{array}\right]The same trick draws a matrix with a horizontal rule (\hline) or splits off several columns, for example {ccc|ccc} for the layout used when inverting a matrix by row reduction:
\left[\begin{array}{ccc|ccc}
1 & 2 & 3 & 1 & 0 & 0 \\
0 & 1 & 4 & 0 & 1 & 0 \\
5 & 6 & 0 & 0 & 0 & 1
\end{array}\right]Row operations are often written beside an arrow: \xrightarrow{R_2 - 2R_1} gives .
Determinants
\det A = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bcUse vmatrix rather than typing | characters around a matrix: the bars then grow with the content and get the right spacing.
Small matrices inside a line of text
A full pmatrix inside a paragraph pushes the lines apart. smallmatrix sets the entries at script size so the matrix fits the line height, like here. It has no brackets of its own:
$\left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right)$With the mathtools package you can write psmallmatrix, bsmallmatrix and the others instead.
Block matrices
Treat each block as an entry. Bold or capital letters keep blocks distinct from scalars:
M = \begin{pmatrix}
A & B \\
C & D
\end{pmatrix}, \qquad
\begin{pmatrix}
A & \mathbf{0} \\
\mathbf{0} & I_k
\end{pmatrix}To draw the partition lines, use array as for augmented matrices, with | in the column specification and \hline between rows:
\left(\begin{array}{c|c}
A & B \\ \hline
C & D
\end{array}\right)Aligning entries and adjusting spacing
Matrix entries are centred. Columns of numbers with minus signs look ragged, so right-align them with the starred environments from mathtools, which take an optional alignment:
\usepackage{mathtools} % in the preamble
\begin{pmatrix*}[r]
-1 & 3 \\
2 & -10
\end{pmatrix*}To loosen a crowded matrix, for example one full of fractions, increase the row spacing locally and widen the gap between columns:
{\renewcommand{\arraystretch}{1.4} % rows 40% taller
\setlength{\arraycolsep}{8pt} % default is 5pt
\begin{pmatrix}
\frac{1}{2} & \frac{1}{3} \\
\frac{1}{4} & \frac{1}{5}
\end{pmatrix}}The outer braces keep the change local to this matrix. In fraction-heavy matrices, \tfrac or a slash (1/2) often reads better than \frac.
Matrices with more than 10 columns
amsmath matrices allow at most 10 columns by default. An 11th column gives the error “Extra alignment tab has been changed to \cr”. Raise the limit in the preamble:
\setcounter{MaxMatrixCols}{20}For very wide matrices, consider whether the reader needs every entry: a general pattern with dots, or a description in words, is usually clearer.
Labelling rows and columns
To label rows and columns, for example to name the states of a transition matrix, the simplest portable method is an array with the labels in an extra row and column:
\begin{array}{c|ccc}
& s_1 & s_2 & s_3 \\ \hline
s_1 & 0.5 & 0.5 & 0 \\
s_2 & 0 & 0.2 & 0.8 \\
s_3 & 1 & 0 & 0
\end{array}For true bordered matrices, with brackets around the entries but not the labels, the blkarray and kbordermatrix packages on CTAN do the job; check that your venue’s class allows them.
Common errors
- “Missing $ inserted”: the matrix is outside maths mode. Wrap it in
\[ … \]. - “Environment pmatrix undefined”:
\usepackage{amsmath}is missing. - “Extra alignment tab has been changed to \cr”: a row has more
&than columns allowed (more than 10, or more than thearrayspecification lists). - A blank row at the bottom: there is a
\\after the last row. Remove it. - Wrong brackets size: you typed
( … )around amatrix. Usepmatrix, or\left( … \right).
tabular code with booktabs rules.
Related guides
- LaTeX paper templates, including a minimal preamble that already loads amsmath.
- How to format a CS conference or workshop paper.
- The maths these matrices are for: vectors and embeddings and attention, both built on matrix products.
References
- American Mathematical Society, User’s Guide for the amsmath Package, version 2.1, 2020. Section 4.1, matrices.
- M. Høgholm, L. Madsen, W. Robertson and J. Wright, The mathtools package, package documentation.
- F. Mittelbach and U. Fischer, The LaTeX Companion, 3rd ed., Addison-Wesley, 2023. Chapter 11, mathematics.