Align multiline equations
Use amsmath alignment points, line breaks, and numbering for readable derivations.
On this page
Use the amsmath environments to break a derivation where its logic changes, not merely where the source line becomes long. Choose the environment according to alignment and numbering needs.
Choose the right environment#
| Environment | Use it for |
|---|---|
align / align* |
Several related lines aligned at relations; the starred form has no numbers. |
aligned |
A compact aligned block nested inside equation or another math container. |
split |
One long numbered equation broken across lines, with one alignment column. |
gather / gather* |
Separate centered equations that need no shared alignment point. |
cases |
A piecewise definition with a left brace and aligned conditions. |
Build and cite a derivation#
Put & immediately before the relation to align and \\ between rows. Punctuate each row as part of the surrounding sentence.
\documentclass[10pt]{article}
\usepackage{amsmath}
\begin{document}
The least-squares objective satisfies
\begin{align}
J(\theta) &= \sum_{i=1}^{n}(y_i-\theta x_i)^2, \label{eq:objective}\\
\frac{dJ}{d\theta} &= -2\sum_{i=1}^{n}x_i(y_i-\theta x_i), \notag\\
\hat{\theta} &= \frac{\sum_i x_i y_i}{\sum_i x_i^2}. \label{eq:estimate}
\end{align}
Equation~\eqref{eq:estimate} is valid when \(\sum_i x_i^2>0\).
A clipped response is defined by
\begin{equation}
g(x)=\begin{cases}
0, & x<0,\\
x, & 0\leq x\leq 1,\\
1, & x>1.
\end{cases}
\end{equation}
\end{document}The first and third derivation lines receive separate labels and numbers; \notag suppresses the middle number. The piecewise function uses a second alignment column for conditions. Put each \label on the row it names.
Use nested and nonaligned layouts#
The examples below illustrate layout. The final gradient and Hessian conditions are necessary conditions at an unconstrained local minimum of a twice continuously differentiable function; they do not by themselves prove a minimum.
Use aligned when several relations should share one equation number:
\begin{equation}
\begin{aligned}
a &= b+c,\\
d &= e-f.
\end{aligned}
\end{equation}Use split for one long equation with a natural continuation, and gather when multiple centered equations are related but have no useful common relation:
\begin{equation}
\begin{split}
p(y\mid x) &= \int p(y,z\mid x)\,dz \\
&= \int p(y\mid z,x)p(z\mid x)\,dz.
\end{split}
\end{equation}
\begin{gather*}
\nabla f(x^*)=0, \\
\nabla^2 f(x^*) \succeq 0.
\end{gather*}The starred form of gather suppresses all numbers. split inherits the single number from its surrounding equation.
Never put align, gather, or equation inside \[...\]; each already enters display math. Multiple & marks create multiple alignment columns and alternating spacing, so use them only for a deliberate layout such as paired equations. If LaTeX reports an alignment error, inspect the first reported row for an extra &, a missing \\, or an unmatched math boundary.
As an exercise, remove the number from the objective row and cite only the estimate. You are done when the remaining reference resolves and punctuation reads naturally across the display. Next, learn to typeset theorems and proofs.
