How to align equations in LaTeX
Load amsmath and usealign. Put &before the character to line up on, and end each row with\\.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
(a+b)^2 &= (a+b)(a+b) \\
&= a^2 + 2ab + b^2
\end{align}
\end{document}Both rows line up on the equals sign, because the & sits just before it. align is a math environment of its own. Do not put it inside equation or $ $.
The alignment environments
| Environment | Use |
|---|---|
| align | Several equations, aligned on a chosen point, each numbered. |
| align* | The same, with no numbers. |
| gather | Several equations, each centred, no alignment. |
| multline | One long equation broken over lines. |
| split | One long equation aligned, inside equation, with a single number. |
| aligned | An aligned block inside another math environment. |
Numbering and references
align numbers every row. Suppress a number with\notag on that row, or use align* to suppress all of them. Add \label{...} to a numbered row and refer to it with \eqref{...}, which adds the parentheses for you.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
f(x) &= x^2 + 2x + 1 \label{eq:expanded} \\
&= (x+1)^2 \label{eq:factored} \\
f(-1) &= 0 \notag
\end{align}
By \eqref{eq:factored}, the root is $x=-1$.
\end{document}Compile twice so the numbers resolve. After the first run you will see?? where the references go.
Several columns of equations
Each extra pair of & adds a column. The first& in a pair aligns, the second separates columns. Use it to set two equations side by side.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
x + y &= 3 & x - y &= 1 \\
2x &= 4 & y &= 1
\end{align*}
\end{document}One number for several rows
Use aligned inside equation, orsplit. Both give a single number, centred vertically on the block.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{aligned}
a &= b + c \\
&= d
\end{aligned}
\end{equation}
\begin{equation}
\begin{split}
a &= b + c \\
&= d
\end{split}
\end{equation}
\end{document}Use split as the whole body of the equation. Use aligned when the block must sit beside other material, such as a brace or a second block.
Centred rows with gather
When the equations have nothing to line up, gather centres each row. It has no &.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{gather*}
x = 1 \\
y + z = 22
\end{gather*}
\end{document}Common mistakes
- "Environment align undefined". Load
amsmath. - Using
eqnarray. It is obsolete and spaces the relation badly. Usealign. - "Erroneous nesting of equation structures." You put
aligninsideequationor\[ \]. Usealignedthere. - "allowed only in paragraph mode." You put
aligninside$ $. Same fix. - "allowed only in math mode." You used
alignedon its own. It needs an enclosing math environment. - "Paragraph ended before ... was complete." There is a blank line inside the environment. Remove it, or put
%on it if you want a gap in the source. - Words in a row. Wrap them in
\text{...}, as in&\quad \text{for all } x.
Templates that use this
Multi-line derivations belong in papers and dissertations. See theamsart article, thesimple thesis, or theacademic journal templates.
Related
The right-hand sides usually containfractions,sums and integrals ormatrices. Browsethesis templates andarticle templates for documents that set up equation numbering already, or start withLaTeX for beginners.