The hard part of a multi-line display in LaTeX is not deciding where to break the lines. It is how many equation numbers the result carries: align gives one per line, multline gives one to the whole formula, and split carries none at all and borrows one from whatever encloses it. Get that wrong and every number after it shifts, taking your cross-references with it. This page sorts out the amsmath environments — equation, align, gather, multline, split and cases — along that one axis, then covers numbering control with \notag, \tag and subequations, and referring back with \label and \eqref. It ends with eqnarray, and with a measurement showing why you should never use it again.
Load amsmath first, and never leave a blank line inside
Every environment on this page except equation comes from amsmath, so one line in the preamble is the precondition for all of it. amsmath fills in a region where plain LaTeX had almost nothing, adding align and align*, gather and gather*, multline and multline*, alignat and alignat*, flalign and flalign*, and split. For equation numbers at the left margin pass \usepackage[leqno]{amsmath}; at the right (the default for horizontal writing) [reqno]; and for flush-left displays, [fleqn].
\usepackage{amsmath}Before anything else, defuse the first mine. Never put a blank line inside these environments. Do it and you get ! Paragraph ended before \align was complete. The reason is historical. The AMS technical notes explain that these display environments read their contents as a delimited macro argument and process them in several passes to work out widths and positions — using algorithms inherited from Michael Spivak’s amstex.tex. A blank line is a \par to TeX, and a \par cannot cross the argument of a macro that is not declared \long. The error message is direct evidence that amsmath still carries the blood of 1980s AMS-TeX.
A numbered single line: the equation environment
The starting point is the equation environment. It sets its contents on a line of their own and adds a running number at the right margin (the default for horizontal writing). The number is kept by the equation counter, and a \label{…} lets you call it from the text with \ref{…} or \eqref{…}. If you only want the number gone, use the starred equation* — which, once amsmath is loaded, is exactly the same thing as \[ … \]. In short, equation gives one number to one line’s worth of formula, no more and no less.
\begin{equation}
\label{eq:euler}
e^{i\pi} + 1 = 0
\end{equation}This sets Euler’s identity centred on a line of its own with a number such as (1) at the right margin (the number is your document’s running count, not necessarily 1). For a formula that will not fit on one line, or several formulas you want aligned, move on to the environments below. One warning: never nest a numbered environment inside \[ … \]. Writing \[ \begin{align} … \end{align} \] produces ! Package amsmath Error: Erroneous nesting of equation structures; — the outer \[ … \] is simply redundant.
Lining up several formulas: align versus gather
align lines things up at the relation; gather stacks them centred and lines up nothing. That is the whole difference, and both number every line (the starred align* and gather* do not). In align you separate lines with \\ and place one & at the point to be aligned — by convention immediately before a relation such as an equals or inequality sign. Everything left of & is set flush right and everything after it flush left, so the two sides meet exactly at the relation. The amsmath manual states plainly that putting & after the relation interferes with the normal spacing — which is precisely how the old eqnarray was designed.
\begin{align}
(x + y)^2 &= x^2 + 2xy + y^2 \\
(x - y)^2 &= x^2 - 2xy + y^2
\end{align}This stacks the two formulas with both equals signs at the same horizontal position and a number at the right of each line. Do not put a \\ after the final line — the amsmath manual warns against it explicitly, because it adds unwanted vertical space below the display. The commonest slip is typing & inside gather: there is no alignment column there, so you get ! Extra alignment tab has been changed to \cr. The same message appears if you put two & on one line of a split.
align can also set several formulas side by side. The & doubles as alignment mark and column separator: one group is “flush-right part & relation-and-flush-left part”, and another & separates it from the next group. amsmath inserts a fixed gap between the columns automatically. If you want to control that gap yourself, use alignat, which takes the number of column pairs as an argument and leaves no space between them so you can insert your own with \quad; and use flalign when you want the columns pushed out to the full width of the text block.
\begin{align}
x &= y & X &= Y & a &= b + c \\
x' &= y' & X' &= Y' & a' &= b \\
x + x' &= y + y' & X + X' &= Y + Y' & a'b &= c'b
\end{align}gather, by contrast, has no alignment at all: each line is centred independently. Lines are separated by \\ and each gets a number (gather* gives none). Remember align as “line them up at the relation” and gather as “just stack them centred” and the choice makes itself. A single line of a gather may contain a split (below) so that this one line breaks across several.
\begin{gather}
a^2 + b^2 = c^2 \\
e^x = \sum_{n=0}^{\infty} \frac{x^n}{n!}
\end{gather}When a long align meets a page boundary, LaTeX will not break it by default: amsmath sets \interdisplaylinepenalty to its maximum. In a document carrying derivations dozens of lines long, writing \allowdisplaybreaks in the preamble relaxes the prohibition. The optional argument runs from [1] to [4], from grudging to permissive, and the default is the most permissive [4]. To forbid a break after one particular line, write that line’s \\ as \\*.
Breaking one long formula: multline and split
Two environments break a single formula that will not fit on a line, and both differ decisively from align/gather: one formula, one number. multline does not align anything. It sets the first line flush left and the last line flush right, with any middle line centred. “Flush left” is not quite flush: measured, the first line starts 10pt inside the margin, the default value of \multlinegap. The number for the whole formula appears on the last line (the first, under leqno). Because it is one formula with one number, you must not blank out individual lines with \notag. To push a middle line to one side, hand the whole line — not the following \\ — to \shoveleft{…} or \shoveright{…}. The unnumbered form is multline*.
\begin{multline}
a + b + c + d + e + f \\
+ g + h + i + j + k + l + m + n
\end{multline}When the broken lines should be aligned, use split. Like align, split aligns at &, but it allows only one alignment column (one & per line) and carries no number of its own. Put it inside a numbered structure such as equation (or align, or gather), and the whole receives a single number from its host. Used on its own it gives ! Package amsmath Error: \begin{split} won't work here. The body of a split should fill the whole of its host; only commands that produce no output, such as \label, may sit beside it. It cannot go inside multline.
\begin{equation}
\label{eq:cosh}
\begin{split}
\cosh x &= \frac{e^x + e^{-x}}{2} \\
&= 1 + \frac{x^2}{2!} + \frac{x^4}{4!} + \cdots
\end{split}
\end{equation}| Environment | Formulas and numbers | Alignment | Main use |
|---|---|---|---|
equation | one / one | none | a single displayed line |
align | many / per line | at & | stack and line up at relations |
gather | many / per line | none (each centred) | stack centred, no alignment |
multline | one / one (on the last line) | none (left to right) | break a long formula, unaligned |
split | one / number comes from the host | one & column | align the lines of one broken formula |
Piecewise definitions: cases
To define a function by cases, use the cases environment. The large left brace appears automatically; within each row a & separates the value from the condition, and rows are separated by \\. Write the words of a condition (“if …”) bare and they come out slanted and tightly spaced, because they are still in math mode — wrap them in \text{…}. cases carries no number itself, so it usually goes inside an equation that gives the whole one number.
\begin{equation}
|x| =
\begin{cases}
x & \text{if } x \ge 0, \\
-x & \text{if } x < 0.
\end{cases}
\end{equation}This puts two rows to the right of a large brace — x (condition x ≥ 0) and −x (condition x < 0) — with a single number from equation. Both the value column and the condition column are flush left. One pitfall: cases sets its contents in text style. Measured, the digits of a \frac{1}{2} inside cases are 6.19pt tall against 8.85pt for the same fraction in an ordinary display. If you want the fraction full size, use dcases from the mathtools package, the same environment set in display style.
Controlling the numbers: \notag, \tag, subequations
In the numbered environments each line is numbered automatically, and you can adjust that line by line. One fact is worth knowing for peace of mind: a line with \notag on it does not consume a number. amsmath redefines \nonumber so that suppressing a line also steps the counter back by one — amsmath.sty literally contains \addtocounter{equation}\m@ne. Put \notag on the second of three align lines and the numbers run (1), none, (2), and the next display is (3). No hole is left in the sequence.
- Drop the number on one line: put
\notagbefore that line’s\\. Its synonym is\nonumber; in amsmath,\notagis literally defined as an alias for\nonumber. - Choose the number yourself:
\tag{…}typesets whatever you give in place of the automatic number and supplies the parentheses.\tag*{…}adds no parentheses and prints your argument literally.\tag{$\star$}yields (⋆);\tag*{NP}yields NP. - Group related formulas as (4a), (4b): wrap them in the
subequationsenvironment and the inner formulas take the parent number plus a letter. A\labelplaced right after\begin{subequations}refers to the parent number (4), not the child (4a). - Restart the numbering per chapter or section: write
\numberwithin{equation}{section}in the preamble and the first equation of section 2 becomes (2.1). It is the standard way to stop equation numbers reaching four digits in a long document.
\begin{align}
y &= (x + 1)^2 \notag \\
&= x^2 + 2x + 1 \\
E &= mc^2 \tag{$\star$}
\end{align}
\begin{subequations}
\label{eq:max}
\begin{align}
\nabla \cdot \mathbf{E} &= \frac{\rho}{\varepsilon_0} \label{eq:gauss} \\
\nabla \cdot \mathbf{B} &= 0
\end{align}
\end{subequations}In the align above, the first line is unnumbered thanks to \notag, the second takes its ordinary automatic number, and the third gets (⋆) in place of one. Leaving the intermediate steps of a derivation unnumbered and numbering only the conclusion is the commonest pattern in a paper. In the subequations below it, the two Maxwell equations are numbered (4a) and (4b); \eqref{eq:gauss} then returns (4a), while \eqref{eq:max} returns (4), standing for the group as a whole.
Referring back with \label and \eqref
Tag a numbered formula with \label{…} and you can call it by number from the text. \ref{…} returns the bare number (say 3.2), while amsmath’s \eqref{…} supplies the parentheses and returns (3.2). In amsmath.sty the whole of \eqref is wrapped in \textup{…}, so the parentheses stay upright even inside italic surroundings — a difference you notice the first time you cite an equation from inside a theorem statement. In a multi-line environment, put \label on the line whose number you want. References travel through the auxiliary file, so you compile twice to settle them.
By~\eqref{eq:euler}, we have $e^{i\pi} = -1$.
% preamble: number equations within sections, giving (2.1), (2.2), ...
\numberwithin{equation}{section}The look of the number itself is decided by \theequation. In article the default is \arabic{equation}; in book and report the definition reads “if the chapter counter is greater than zero, prefix \thechapter., then the running count” — that is literally the line in book.cls, which is why front matter gets a bare number with no chapter prefix. You can redefine the format directly, for instance with \renewcommand{\theequation}{\thesection.\arabic{equation}}, but \numberwithin is usually the safer choice because it also arranges for the counter to be reset.
Never use eqnarray: it is 3.6 times too loose
Older documents use eqnarray (and eqnarray*) for multi-line math. Do not. The reason can be given as a measurement rather than an opinion. Look at the definition of \eqnarray in latex.ltx and you find \hskip \tw@\arraycolsep on both sides of the relation column — and \arraycolsep defaults to 5pt, so that is 10pt a side. Set the same formula in align or equation and each side gets \thickmuskip, which is 2.78pt. Measured in a 10pt document: 9.97pt against 2.77pt, a factor of 3.6. Worse, that 10pt is a rigid dimension inherited from table column spacing, so it does not scale when the math font size changes.
The second real harm is the equation number. Put a long line in an eqnarray and the number prints on top of the formula. Measured, the last term occupied horizontal positions 483 to 505pt while the number (1) occupied 492 to 505pt — squarely overlapping. Put the same content in align and amsmath detects the collision and drops the number to a line of its own. Those are exactly the two reasons the AMS Short Math Guide for LaTeX gives for discouraging eqnarray: inconsistent spacing of the equals signs, and no attempt to prevent the number overprinting the body of the equation. The amsmath manual adds a third: \qed and \qedhere from the theorem packages do not work inside it.
The replacement is mechanical. Several formulas to line up: align. One formula to break: equation plus split. The & moves too — eqnarray was designed to put & after the relation, while the align family puts it before, so x &=& y becomes x &= y. When you inherit a manuscript, that substitution is the first job to do.