Long derivations read better when the equals signs line up. The align environment from amsmath does this. Each & marks an alignment point, and \\ starts a new line.
Basic align
\begin{align*}
f(x) &= (x+1)^2 - 1 \\
&= x^2 + 2x + 1 - 1 \\
&= x^2 + 2x
\end{align*}
The & goes before each =, so the equals signs line up down the page. The lines do not need to be the same length.
align versus align*
alignnumbers every line.align*prints no numbers.\notagor\nonumberremoves the number from one line of analign.\labelon a line gives that line a reference.
\begin{align}
a^2 + b^2 &= c^2 \label{eq:pythagoras} \\
c &= \sqrt{a^2 + b^2} \notag
\end{align}
The first line can be cited in the text as \eqref{eq:pythagoras}. How to label an equation covers references in more detail.
Other amsmath alignment environments
| Environment | Use it for |
|---|---|
aligned |
Alignment inside another math environment, such as equation or \[ \], with one number for the whole block |
gather |
Lines centered on the page, with no alignment point |
multline |
One long equation broken across lines, with the first line flush left and the last line flush right |
split |
One long equation inside equation, with one number |
alignat |
Alignment with several columns and custom spacing |
Systems of equations
Put aligned inside \left\{ ... \right. to get one brace over several equations that line up at the equals sign:
\[
\left\{
\begin{aligned}
2x + y &= 7 \\
x - y &= 2
\end{aligned}
\right.
\]
The \right. closes the brace without printing one. The solution is $x = 3$ and $y = 1$. Sizing brackets and parentheses covers the delimiter pairs in more detail.
Text between aligned lines
\intertext{...} adds a line of text between aligned lines without breaking the alignment:
\begin{align*}
x + y &= 5 \\
\intertext{Substituting $x = 2$ into the first equation gives}
y &= 3
\end{align*}
Common mistakes
- Blank lines inside the environment cause a compilation error. If you want visual space in the source, put a
%on a line by itself. - Too many
&characters in one line add an extra alignment column and produce an “Extra alignment tab” error. - Math mode inside align: do not wrap
alignin$$or\[ \]. The environment is already display math. - eqnarray: the older environment spaces the middle column differently and handles numbering poorly. Use
aligninstead. - Missing amsmath:
alignis not defined without the package, so loadamsmathin the preamble.
For the matrix and determinant environments that often sit inside an aligned block, see matrices and determinants.