The word “matrix” entered mathematics through James Joseph Sylvester in 1850, and its algebra was built by his friend Arthur Cayley in “A Memoir on the Theory of Matrices”, published in volume 148 of the Philosophical Transactions in 1858. In LaTeX, a matrix turns out not to be a special construction at all. Inside amsmath, pmatrix is \array{*10 c} — an array with ten centred columns — wrapped in parentheses, with 5pt shaved off each side. That single line answers the two questions people most often arrive with: why a matrix runs out of columns at the eleventh, and how to change the gap between columns. This page covers the family pmatrix, bmatrix, vmatrix and smallmatrix, the counter MaxMatrixCols, the array environment with \arraycolsep and \arraystretch, augmented matrices, and the dots \cdots, \vdots, \ddots.
amsmath defines exactly seven matrix environments
They are matrix, pmatrix, bmatrix, Bmatrix, vmatrix, Vmatrix and smallmatrix. Write \usepackage{amsmath} 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 contents are written in exactly the same way; the only difference is the delimiter wrapped around them. The naming is regular too: the leading letter names the delimiter — p for parenthesis, b for bracket, v for vertical bar. The capital-initial Bmatrix and Vmatrix give the “stronger” pair, braces and double bars. Plain matrix adds nothing at all.
| Environment | Delimiter added | Typical use |
|---|---|---|
matrix | none | a base for 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 |
% preamble: \usepackage{amsmath}
\[
\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}
\]The contents of all seven are always centred; you cannot ask for right- or left-aligned columns. When you want to choose the alignment yourself, use the array environment described below, or the starred variants from the mathtools package. mathtools defines matrix*, pmatrix*, bmatrix*, Bmatrix*, vmatrix*, Vmatrix* and smallmatrix*, each taking an optional [r], [c] or [l] that aligns every column that way (the default is c). The starred forms come from mathtools, not amsmath, so you need \usepackage{mathtools} — and since mathtools loads amsmath itself, you do not need both lines. Stretching delimiters by hand with \left/\right or \big/\Big is the business of the separate page “Delimiters”.
Matrix has too many columns: MaxMatrixCols and the eleventh column
The default ceiling is 10 columns, and an eleventh stops the run with ! Extra alignment tab has been changed to \cr. TeX goes on to explain: You have given more \span or & marks than there were in the preamble to the \halign or \valign now in progress. Read it and you can see this is not a “matrix” error at all but the generic alignment error of \halign. As noted above, the inside of pmatrix is \array{*\c@MaxMatrixCols c}, and line 1077 of amsmath.sty — \newcount\c@MaxMatrixCols \c@MaxMatrixCols=10 — is what fixes the ceiling. No special matrix check is running; you have simply used up the column specification handed to array.
The fix is to raise the ceiling with \setcounter{MaxMatrixCols}{n}. To allow up to twenty columns, write \setcounter{MaxMatrixCols}{20}. A larger value makes the column specification handed to array correspondingly longer, so TeX does more work, but at around 20 there is no practical impact on a modern system. Set it back to 10 to restore the original. Note that this ceiling applies only to the amsmath matrix environments, not to the array of the next section. Indeed, \begin{array}{ccccccccccccccc} with fifteen columns compiled without a single error — in array you simply write as many columns as you need.
% without this line, an 11th column raises
% ! Extra alignment tab has been changed to \cr.
\setcounter{MaxMatrixCols}{20}
\[
\begin{pmatrix}
a_{1} & a_{2} & \cdots & a_{12}
\end{pmatrix}
\]The array environment: choose the alignment and rules yourself
array is a general grid that takes a column specification as a required argument — one letter per column: l for left-aligned, c for centred, r for right-aligned. Put a | between letters and a vertical rule is drawn at that position, so {l|c|r} means three columns: left, rule, centred, rule, right. It is the math-mode counterpart of tabular, the environment for text tables, and it can be used only in math mode. Write \begin{array} outside \[ \] or an equation environment and LaTeX tries to enter math mode for you, reporting ! Missing $ inserted. Entries are separated with & and \\ just as in tabular, but each entry is set as mathematics, in text style.
array adds no delimiter of its own, so to wrap it in brackets as a matrix you write \left( … \right) (or \left[ … \right]) by hand. Here the “5pt shaved off each side” from the opening can be checked by measurement. With the contents a & b \\ c & d in both, the box for \left(\begin{array}{cc} … \end{array}\right) is 45.21304pt wide and pmatrix is 35.21304pt. The difference is exactly 10pt — two helpings of \arraycolsep, the padding array puts at each edge of a column, which defaults to 5pt. The amsmath matrix environments cancel it with a \hskip -\arraycolsep at the start and at the end. Turn that around and changing \arraycolsep changes the column gaps of a matrix too: measured, shrinking it to 2pt made pmatrix 29.21304pt wide, and widening it to 10pt made it 45.21304pt. To stretch every row uniformly, use \renewcommand{\arraystretch}{1.5} — at 1.5 the total height of a 2×2 went from 24pt to 36pt, exactly one and a half times (the width is unaffected).
% array needs math mode; outside it LaTeX reports "! Missing $ inserted."
\[
\left(\begin{array}{rrr}
1 & -2 & 3 \\
0 & 5 & -1 \\
4 & 0 & 2
\end{array}\right)
\]The column specification here is {rrr}, so every column is right-aligned and negative entries such as -2 and -1 line up at the right edge, which reads cleanly. pmatrix, always centred, cannot do this; it is usually the reason to reach for array at all.
Augmented matrices: one vertical rule partway across
Put a | in the column specification of an array and wrap the whole thing in \left[ … \right]. In an augmented matrix — the kind that represents a system of linear equations — convention puts a vertical rule between the coefficient columns and the constant column. The amsmath environments such as bmatrix take no column specification and so cannot draw one; this is where array earns its keep. A specification with a | between the coefficient and constant columns (for example {cc|c}) is all it takes. For parentheses switch to \left( … \right), for braces \left\{ … \right\}. Because \left and \right stretch the delimiter to the height of the contents, the shape holds up as you add rows (the stretching mechanism itself belongs to the separate page “Delimiters”). mathtools also provides tidier ways to write this kind of augmented matrix.
\[
\left[\begin{array}{cc|c}
1 & 2 & 5 \\
3 & 4 & 6
\end{array}\right]
\]Writing a general n×n matrix: \cdots, \vdots, \ddots
Place the ellipsis that means “and so on” as an entry of the matrix. There are four, differing in direction, and you choose among them by where they fall in the grid. All are math-mode commands and need no extra package.
\cdots— horizontal dots at the centre 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 sits better.
\[
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 is the canonical way to write a general m×n matrix. In the first, second and last rows the \cdots in the third column fills the gap between a_{12} and a_{1n}; in the third row the \vdots give the vertical omission in each column while the \ddots at the diagonal position gives the one descending to the right — together saying that both rows and columns continue likewise. To fill an entire row with dots, amsmath’s \hdotsfor{n} spans the specified n columns with a row of horizontal dots.
A matrix inside a line of text: smallmatrix fits in exactly one line
Dropping a pmatrix into an inline $ … $ swells the line height and disturbs the surrounding leading. The numbers make the reason plain. At 10pt, a 2×2 matrix inside \bigl( … \bigr) is 14.5pt high and 9.5pt deep, 24.0pt in total — exactly two helpings of the 12pt \baselineskip. amsmath’s smallmatrix environment sets the entries small and compact, and the same contents in the same brackets come out 8.5pt high and 3.5pt deep, 12.0pt in total: exactly one line. smallmatrix adds no delimiter, so wrap it by hand if you need one, e.g. \bigl( … \bigr). (You can use \left( \right) too, but inline the fixed-size \bigl and \bigr disturb the leading less.) If mathtools is loaded, smallmatrix* is available as well, letting you choose the column alignment.
% preamble: \usepackage{amsmath}
Consider the rotation matrix
$\bigl(\begin{smallmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{smallmatrix}\bigr)$
in the plane.