Matrices, vectors, and any mathematics laid out in a grid are set either with the matrix environments of the amsmath package (pmatrix and friends) or with the more general **array environment**. Both share the same feel — entries separated by & across and by \\ down — but they differ in the delimiters they add and in how freely you can control alignment. This page works through the family of matrix environments and when to reach for each, the array environment for choosing your own column alignment and rules, how to build an augmented matrix, and the dots — \cdots, \vdots, \ddots — that say “and so on” inside a matrix.
The amsmath matrix environments
The easiest way to set a matrix is the family of matrix environments provided by the amsmath package; put \usepackage{amsmath} in your preamble and they are available. Each is used inside math mode, with entries **separated across a row by & (an ampersand) and rows ended by \\ (two backslashes). The only thing that differs between them is the delimiter** wrapped around the matrix — the contents are written the same way.
The names follow a pattern. The leading letter names the delimiter: p for parentheses, b for brackets, v for vertical bar. The capital-initial Bmatrix and Vmatrix give the “stronger” braces and double bars. Plain matrix adds no delimiter at all.
| Environment | Delimiter added | Typical use |
|---|---|---|
matrix | none | A base when you will add your own delimiter |
pmatrix | parentheses ( ) | The most common matrices and column vectors |
bmatrix | brackets [ ] | Matrices (in the bracket convention) |
Bmatrix | braces { } | When you want curly braces |
vmatrix | single bars | | | Determinants |
Vmatrix | double bars ‖ ‖ | Norms |
smallmatrix | none (compact) | A small matrix set inline within text |
The example below sets the same 2×2 contents with pmatrix, bmatrix, and vmatrix. Whitespace and line breaks around \\ do not affect the output, so you may format the source for readability.
\[
\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
\quad
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
\quad
\begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
\]This outputs three 2×2 matrices centered on their own line, spaced apart by \quad: the first wrapped in parentheses, the second in square brackets, and the third flanked by vertical bars as a determinant. The entries are centered, and a, b, c, d are slanted as variables.
Note that entries in the matrix environments are always centered — you cannot ask for right- or left-aligned columns. When you need to choose the alignment yourself, use the array environment described below.
The column limit (MaxMatrixCols)
By default the amsmath matrix environments allow at most 10 columns. This is because the counter MaxMatrixCols is initially set to 10. If you try to set a matrix with eleven or more columns, you get an error about too many columns.
When you need more, raise the limit with \setcounter{MaxMatrixCols}{n}. To allow up to 20 columns, for example, write the following. Larger values make LaTeX work harder, but on today’s TeX systems a value around 20 has essentially no practical impact. To restore the original, set the value back to 10.
\setcounter{MaxMatrixCols}{20}
\[
\begin{pmatrix}
a_{1} & a_{2} & \cdots & a_{12}
\end{pmatrix}
\]This limit applies only to the amsmath matrix environments, not to the array environment described next; with array you simply list as many columns as you need in the column specification.
The array environment
The array environment is a general grid in which you choose the alignment and rules yourself. It is the math-mode counterpart of the tabular environment used for text tables, and it can be used only in math mode (inside \[ \] or an equation environment). Entries are separated with & and \\ just as in tabular, but each entry is set as mathematics, in text style.
The array environment takes a required column specification argument. You write one alignment letter per column, as in {ccc}: l for left-aligned, c for centered, r for right-aligned. Putting a | between letters draws a vertical rule at that position. So {l|c|r} means three columns — left, a rule, centered, a rule, right.
The array environment adds no delimiter of its own. To wrap it in brackets as a matrix, enclose it by hand in **\left( … \right)** (or \left[ … \right], and so on). Because \left and \right stretch the delimiter to the height of their contents, the brackets neatly span the whole matrix even when it has many rows. This is the manual version of exactly what the amsmath matrix environments do for you internally.
\[
\left(\begin{array}{rrr}
1 & -2 & 3 \\
0 & 5 & -1 \\
4 & 0 & 2
\end{array}\right)
\]This outputs a 3×3 matrix in parentheses. Because the column specification is {rrr}, every column is right-aligned, so negative entries like -2 and -1 line up at the right and read cleanly. The point is that, unlike pmatrix (always centered), array lets you choose the alignment.
Augmented matrices (with a rule)
For an augmented matrix — the kind that represents a system of linear equations — you often want a vertical rule partway through. The amsmath environments such as bmatrix take no column specification and so cannot draw one, but the | in an array column specification can. Give a specification with a | between the coefficient columns and the constant column (for example {cc|c}) and wrap the whole thing in \left[ … \right].
\[
\left[\begin{array}{cc|c}
1 & 2 & 5 \\
3 & 4 & 6
\end{array}\right]
\]This outputs an augmented matrix in square brackets — two rows with a single vertical rule between the second and third columns. The 1 2 / 3 4 on the left is the coefficient matrix and the 5 / 6 to the right of the rule is the constant column. Because \left[ and \right] stretch the brackets to the height of the contents, it holds up as rows are added.
To do the same with parentheses, switch to \left( … \right); for braces, \left\{ … \right\}. (The mathtools package also offers tidier ways to write such augmented matrices.)
Dots inside matrices (…, ⋮, ⋱)
To write a general n×n matrix you need the ellipses that mean “and so on.” LaTeX provides four, differing in direction, and you choose among them by where they fall in the matrix. All are math-mode commands.
\cdots— horizontal dots at the center height (⋯), for omitting entries along a row. They sit at the height of+and=, which suits a horizontal run.\vdots— vertical dots (⋮), for omission down a column.\ddots— diagonal dots descending to the right (⋱), for omission along the main diagonal.\ldots(and the context-aware\dots) — horizontal dots on the baseline (…), for runs of numbers or subscripts. Inside a matrix,\cdotsusually looks better.
The standard usage is to place \cdots for horizontal omission, \vdots for vertical, and \ddots for the diagonal, each as an entry of the matrix. Below is the canonical way to write a general n×n matrix with pmatrix.
\[
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}
\]This outputs an m×n matrix in parentheses. In the first, second, and last rows the \cdots in the third column fills the gap between a_{12} and a_{1n} with center-height horizontal dots; in the third row the \vdots give vertical dots in each column while the \ddots at the diagonal position gives dots descending to the right — together saying that both rows and columns continue likewise. Subscripts like a_{11} are set low via _.
To fill an entire row with dots, amsmath’s \hdotsfor{n} spans the specified n columns with a row of horizontal dots.
Small matrices inline (smallmatrix)
Dropping pmatrix and the like into inline math (a $ … $ in running text) swells the line height and disturbs the surrounding leading. To embed a small matrix in the flow of text, use amsmath’s **smallmatrix environment**, which sets the entries small and compact and barely changes the line height.
The smallmatrix environment adds no delimiter, so wrap it by hand if you need one, e.g. \bigl( … \bigr). (You can also use \left( \right), but inline the fixed-size \bigl and \bigr often disturb the leading less.)
回転行列 $\bigl(\begin{smallmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{smallmatrix}\bigr)$ を考える。This sets a small 2×2 rotation matrix in parentheses within a single line of text, without much disturbing the line height. \cos and \sin are set upright as function names.