How to write a matrix in LaTeX
Load amsmath, then use thepmatrix environment. Columns are separated by & and rows end with\\.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
A = \begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix}
\]
\end{document}That is the whole pattern. The matrix environments only work in math mode, so put them inside \[ ... \],$ ... $ or an equation environment.
Choosing the brackets
amsmath provides six environments. They differ only in the delimiters drawn around the grid.
| Environment | Delimiters | Typical use |
|---|---|---|
| matrix | none | Bare grid. Add your own delimiters. |
| pmatrix | ( ) | Parentheses. The usual choice. |
| bmatrix | [ ] | Square brackets. |
| Bmatrix | { } | Curly braces. |
| vmatrix | | | | Single bars: a determinant. |
| Vmatrix | ‖ ‖ | Double bars: a norm. |
\[
\det A = \begin{vmatrix}
a & b \\
c & d
\end{vmatrix} = ad - bc
\]Large matrices with dots
Use \cdots for a horizontal run,\vdots for a vertical one and\ddots for the diagonal. All three are in core LaTeX math mode.
\[
A = \begin{bmatrix}
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{bmatrix}
\]Augmented matrices
The matrix environments have no way to draw a vertical rule. Use the core array environment instead. Its argument sets the column alignment, and a | in it draws the line. Draw the brackets yourself with \left[ ... \right].
\[
\left[
\begin{array}{cc|c}
1 & 2 & 5 \\
3 & 4 & 6
\end{array}
\right]
\]With mathtools loaded (it loads amsmath for you) the starred form \begin{pmatrix*}[r] aligns the columns right, which helps when entries have different widths.
Vectors
A column vector is a matrix with one column. For the symbol itself,\vec{v} draws an arrow and\mathbf{v} sets bold upright type. Both are core LaTeX. Use whichever convention your field uses, and use it everywhere.
\[
\mathbf{v} = \begin{pmatrix} x \\ y \\ z \end{pmatrix},
\qquad
\vec{w} = (w_1, w_2, w_3)
\]For a small matrix inside a line of text, smallmatrix is compact. It has no delimiters, so wrap it:$\bigl(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\bigr)$.
Common mistakes
- "Environment bmatrix undefined." You did not load
amsmath. Load it even if you only usepmatrix: without it, that name falls back to an old plain TeX macro with different behaviour. - "Missing $ inserted." You used the environment outside math mode. Wrap it in
\[ ... \]. - "Extra alignment tab has been changed to \cr." The
matrixenvironments allow at most 10 columns by default. Raise the limit with\setcounter{MaxMatrixCols}{20}in the preamble. - Rows with different lengths. A short row is padded with empty cells, with no error. Check that a missing
&is not a typo.
Templates that use this
Matrices turn up in coursework and papers. Themaths homework template and theamsart article already load the maths packages, and the assignment templatescover problem sets.
Related
Matrix entries are usually subscripted; seesubscripts and superscripts. To line a matrix equation up with others, seealigning equations. For a ready document with the maths already set up, browse thearticle templates, or readLaTeX for beginners.