Written and maintained by CASRAI Editorial Board
Last updated
A matrix in LaTeX is typeset with one of the matrix environments from the amsmath package: pmatrix for parentheses, bmatrix for square brackets, Bmatrix for curly braces, vmatrix for single bars, Vmatrix for double bars, and plain matrix for no delimiters. This guide covers the syntax, the choice between these environments and array, and the limits that most short tutorials leave out, such as the default ten-column cap. It is about LaTeX typesetting only.
What it is used for in research
Matrices appear in research writing wherever an array of quantities has to sit inside an equation: coefficient arrays, transformation arrays, covariance arrays and similar objects. In LaTeX the matrix environments exist so the delimiters, spacing and alignment of those arrays are produced by the document rather than drawn by hand. The environments belong to amsmath, the package documented in the amsmath User’s Guide on CTAN. The same source describes the matrix environments as using more economical horizontal spacing than the general-purpose array environment, which is why they are the usual choice for matrices in equations.
Install or access (official source only)
The amsmath User’s Guide states that amsmath is normally provided along with an up-to-date LaTeX installation, so there is usually nothing to download. Load it in the preamble:
\usepackage{amsmath}
Overleaf’s Learn page on matrices gives the same requirement: the matrix environments need \usepackage{amsmath}. The package documentation itself is hosted on CTAN (see Sources). If your project is on Overleaf, the guide to collaborative writing in LaTeX and Overleaf covers the workflow around the editor.
Step-by-step: typeset a matrix
- Add
\usepackage{amsmath}to the preamble, before\begin{document}. - Choose the environment that has the delimiters you want (see the table in the next section). Delimiters are built in for
pmatrix,bmatrix,Bmatrix,vmatrixandVmatrix;matrixhas none. - Write the entries row by row. Separate columns with
&and end each row with\\. You do not give a column specification, unlikearray. - Place the environment in math mode and compile.
The example below is the form shown on Overleaf’s Learn page:
\begin{pmatrix}
1 & 2 & 3 \\
a & b & c
\end{pmatrix}
To use delimiters that the built-in environments do not offer, Overleaf shows wrapping a plain matrix in \left and \right, for example with \langle and \rangle.
Rows of dots with hdotsfor
Inside a matrix, \hdotsfor{number} produces a row of dots spanning that many columns. The amsmath User’s Guide example is:
\begin{matrix}
a & b & c & d \\
e & \hdotsfor{3}
\end{matrix}
An optional argument scales the dot spacing, as in \hdotsfor[1.5]{3}; the default multiplier is 1.0.
A matrix inside a line of text
For a small matrix in running text, use smallmatrix. According to the amsmath User’s Guide, it is suitable for use in text, and its delimiters must be provided by you. The guide’s example uses \bigl( and \bigr):
\bigl( \begin{smallmatrix} a & b \\ c & d \end{smallmatrix} \bigr)
Overleaf’s page shows the same idea as $\big(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\big)$. Both sources note that the mathtools package provides ready-made delimited versions, such as psmallmatrix and bsmallmatrix, so you do not have to add the delimiters by hand.
Output and how to read it
Each environment differs only in the delimiters drawn around the entries. The delimiters come from the amsmath User’s Guide, section on matrices.
| Environment | Delimiters |
|---|---|
matrix |
None |
pmatrix |
Parentheses ( ) |
bmatrix |
Square brackets [ ] |
Bmatrix |
Curly braces { } |
vmatrix |
Single vertical bars | | |
Vmatrix |
Double vertical bars |
smallmatrix |
None built in; add them yourself or use the mathtools small variants |
Entries in these environments are centered in their columns by default.
Troubleshooting
The matrix stops at ten columns
The matrix environments allow up to 10 centered columns by default. The limit is stored in the counter MaxMatrixCols, whose normal value is 10. Raise it with \setcounter or \addtocounter, for example \setcounter{MaxMatrixCols}{20}.
Entries need left or right alignment
The matrix environments center their columns. The amsmath User’s Guide gives two routes to other alignment: use the array environment, or load mathtools, which provides starred (*) variants of these environments that take an optional argument for left or right alignment. The exact argument syntax is in the mathtools documentation on CTAN, linked in the Sources section.
A small inline matrix has no brackets
That is expected: smallmatrix has no built-in delimiters. Add them with \bigl and \bigr, or use the mathtools variants.
Undefined environment error
If the compiler reports an undefined environment such as pmatrix, check that \usepackage{amsmath} is in the preamble, since the delimited environments are defined there.
Free alternatives (table)
These are the free ways to build the same kind of array.
| Option | Notes |
|---|---|
array environment |
Requires a column specification; uses wider horizontal spacing than the matrix environments; gives control over left and right alignment (amsmath User’s Guide). |
mathtools package |
Starred variants of the matrix environments with an optional alignment argument; delimited smallmatrix variants (amsmath User’s Guide, Overleaf Learn). |
\left and \right around plain matrix |
Lets you choose delimiters the built-in environments do not offer (Overleaf Learn). |
For other LaTeX tasks in the same document, see the guides on figures in LaTeX, citations and bibliographies in Overleaf and LaTeX bibliography styles.
FAQ
How do I put parentheses or brackets around a LaTeX matrix?
Use pmatrix for parentheses or bmatrix for square brackets. Both have their delimiters built in once amsmath is loaded.
How many columns can a LaTeX matrix have?
Up to 10 by default. The limit is the MaxMatrixCols counter, which you can change with \setcounter{MaxMatrixCols}{N}.
How do I write a matrix inside a sentence?
Use smallmatrix and supply the delimiters yourself, or use the mathtools variants psmallmatrix and bsmallmatrix.
What is the difference between matrix and array?
The matrix environments need no column specification and use more economical spacing; array needs a column specification and is the route to left or right alignment.
Sources (official docs)
Checked 2026-10-29.
- amsmath User’s Guide, version 2.1 (revised to 2020-02-18), CTAN, section “Matrices”: https://mirrors.ctan.org/macros/latex/required/amsmath/amsldoc.pdf
- Overleaf Learn, “Matrices”: https://www.overleaf.com/learn/latex/Matrices
mathtoolspackage page on CTAN: https://ctan.org/pkg/mathtools; documentation: https://mirrors.ctan.org/macros/latex/contrib/mathtools/mathtools.pdf








