Guides for Texlet
How to write matrices and aligned equations in LaTeX
Matrices, multi-line derivations and systems of equations all follow the same idea in LaTeX: an environment arranges content in rows and columns, & separates columns, and \\ ends a row. Once that clicks, pmatrix, align and cases are easy.
All environments in this guide come from the amsmath package. In a full document, add \usepackage{amsmath} to the preamble; without it you get errors such as “Environment pmatrix undefined”.
The matrix environments
amsmath has one matrix environment per bracket style. The content is always written the same way, only the delimiters change. All of them are used inside math mode, for example within \[ … \].
| Brackets | Environment |
|---|---|
| Plain, no brackets | matrix |
| Parentheses ( ) | pmatrix |
| Square brackets [ ] | bmatrix |
| Braces { } | Bmatrix |
| Single bars | | (determinant) | vmatrix |
| Double bars ‖ ‖ (norm) | Vmatrix |
| Small, for inline math | smallmatrix |
pmatrix, bmatrix and vmatrix in practice
Write the entries row by row. Separate columns with & and end every row except the last with \\. A vmatrix is the usual way to write a determinant.
\begin{pmatrix}
a & b \\
c & d
\end{pmatrix}
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{bmatrix}
\det(A) = \begin{vmatrix}
a & b \\
c & d
\end{vmatrix} = ad - bcLarge matrices with dots
For general m × n matrices, \cdots draws centered horizontal dots, \vdots vertical dots and \ddots diagonal dots. By default amsmath matrices allow up to 10 columns; for more, set \setcounter{MaxMatrixCols}{20} in the preamble.
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}Augmented matrices and small inline matrices
The matrix environments cannot draw a vertical line. For an augmented coefficient matrix, use the array environment with a column specification such as {cc|c} and wrap it in \left( … \right). Each letter stands for a centered column and | draws the line.
Inside running text, a normal pmatrix is too tall. Use smallmatrix and add the brackets yourself, for example $\bigl(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\bigr)$.
\left(\begin{array}{cc|c}
1 & 2 & 5 \\
3 & 4 & 6
\end{array}\right)Aligning equations with align
align puts several equations below each other and aligns them at the & – usually placed directly before the equals sign. Every line gets a number; align* leaves out the numbers, and \notag removes the number from a single line.
align is a display environment of its own. Do not put it inside \[ … \] or equation; that causes an error. If you need an aligned block inside an existing display, use aligned instead.
\begin{align}
f(x) &= (x + 1)^2 \\
&= x^2 + 2x + 1
\end{align}
\begin{align*}
a &= b + c \\
d &= e - f
\end{align*}
\[
\begin{aligned}
(a + b)^2 &= (a + b)(a + b) \\
&= a^2 + 2ab + b^2
\end{aligned}
\]Case distinctions with cases
cases sets a left brace with two columns: the value and the condition, separated by &. \text{…} keeps words upright and with normal spacing.
|x| = \begin{cases}
x & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}Systems of equations
For a system of linear equations, combine aligned with a left brace. \right. is the invisible partner that \left\{ needs. Thanks to the &, the equals signs line up neatly. If you prefer the matrix form, write the system as an augmented matrix as shown above.
\left\{
\begin{aligned}
2x + 3y &= 7 \\
x - y &= 1
\end{aligned}
\right.Common errors
- “Misplaced alignment tab character &”: an & outside a matrix, align or cases environment, or a typo in the environment name.
- “Environment pmatrix undefined”: amsmath is not loaded.
- Every \begin{…} needs an \end{…} with exactly the same name.
- No \\ after the last row – it can add an empty row or extra space.
- A missing \right. after \left\{ stops the formula from rendering.
Writing matrices on iPhone and iPad
Typing & and \\ on a software keyboard is tedious. Texlet has building blocks for matrices and environments above the keyboard, and pressing Return after \begin{…} adds the matching \end{…}, which avoids the most common error on this list. The live preview typesets matrices and cases as you type, also offline, so you see right away whether a row is missing an entry. With Texlet Pro you can export the result as PDF or PNG or copy it as an image.
Frequently asked questions
What is the difference between pmatrix and bmatrix?
Only the brackets: pmatrix uses parentheses, bmatrix uses square brackets. vmatrix uses vertical bars, as used for determinants. All three are part of amsmath.
Why do I get an error when I put align inside \[ … \]?
align already creates its own display. Inside \[ … \] or equation, use aligned instead, which produces an aligned block within an existing formula.
How do I write a system of equations in LaTeX?
Use \left\{ \begin{aligned} … \end{aligned} \right. and place & before each equals sign. For a function defined piecewise, use the cases environment.