Displayed, aligned & numbered equations

Display math often needs more than a single line. You break a long formula across several lines, line up the equals signs, give each line a running number, and refer back to it later — and almost all of this multi-line machinery is provided by the amsmath package. This page works through the numbered equation; the alignment environments align, gather, multline, and split; the piecewise cases; and the control of numbers (\notag, \tag, subequations). The doorways into inline and display math themselves are covered in “Math mode basics,” so here we go one level deeper, into multiple lines and numbering.

First, load amsmath

Every environment on this page except equation is provided by the amsmath package. It takes one line in the preamble. For any new document that uses mathematics at all, loading amsmath is the de facto standard: it brings the displayed-equation environments, alignment, and numbering control together in one place.

latex
\usepackage{amsmath}

amsmath greatly extends the set of displayed-equation structures beyond basic LaTeX. Alongside the equation environment (which exists in plain LaTeX) it adds align and align*, gather and gather*, multline and multline*, alignat and alignat*, flalign and flalign*, and split. Note that you must not leave a blank line inside any of these environments — a blank line is read as a paragraph break and raises an error. To place equation numbers on the left use \usepackage[leqno]{amsmath}; on the right (the default for horizontal writing) [reqno]; and to set displays flush left, the [fleqn] option.

A numbered single line — equation

The starting point for display math is the **equation environment**. It sets its contents on their own line and automatically adds a running number at the right margin (the default for horizontal writing). The number is tracked by the equation counter, and with \label{…} you can refer to it from the text via \ref{…} or \eqref{…} (referencing is detailed below). If you simply want no number, use the starred **equation***, which is equivalent to basic LaTeX’s displaymath (\[\]): an unnumbered display.

latex
\begin{equation}
  \label{eq:euler}
  e^{i\pi} + 1 = 0
\end{equation}

This outputs Euler’s identity centered on its own line, with a number such as (1) at the right margin of that line (the number is the running count in your document, not necessarily 1). equation gives one number to one line’s worth of formula. For a formula that does not fit on one line, or for several formulas you want aligned, reach for the alignment environments below. The basics of \[\] and unnumbered displays are covered on the sibling page “Math mode basics.”

Aligning several lines — align and gather

To stack several formulas vertically, the workhorse is the **align environment**. Separate the lines with \\ and place one **&** at the point you want lined up. By convention the & goes immediately before a relation such as an equals or inequality sign, so the relations line up vertically. The key is to put the & before the relation (the obsolete eqnarray discussed later puts it after, which spoils the spacing). align numbers every line; the starred **align*** adds no numbers.

latex
\begin{align}
  (x + y)^2 &= x^2 + 2xy + y^2 \\
  (x - y)^2 &= x^2 - 2xy + y^2
\end{align}

This outputs the two formulas stacked vertically with both equals signs lined up at exactly the same horizontal position, and a number at the right margin of each line. The material to the left of & (the left-hand side) is set flush right and the material to its right (the right-hand side) flush left, so the two sides meet cleanly at the equals sign. Do not put a \\ after the final line — it would add unwanted space.

align can also place several formulas side by side. The & serves both as the alignment mark and as the column separator: you alternate “& before the relation” with “& before the next formula.” In other words each formula is written as “flush-right part & relation-and-flush-left part,” and another & separates it from the next formula.

latex
\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}

This outputs three pairs of formulas in three columns across the page, with the equals signs lined up within each column, and one number per row. amsmath inserts a fixed, automatic gap between the columns.

When, by contrast, you need no alignment and just want several formulas centered, use the **gather environment**. In gather there is no & alignment; each line is centered independently. Lines are separated by \\ and each gets a number (gather* for none). A line of gather may itself contain a split (below) to break one formula across lines.

latex
\begin{gather}
  a^2 + b^2 = c^2 \\
  e^x = \sum_{n=0}^{\infty} \frac{x^n}{n!}
\end{gather}

This outputs the two formulas each centered on its own line (their symbols are not aligned with one another), with a number at the right margin of each line. A handy contrast: align “lines things up at the relation,” while gather “just stacks them centered.”

Breaking one long formula — multline and split

Two environments break a single formula that will not fit on one line. They are **multline and split**, and both differ from align/gather in giving one number to the one formula.

multline does no alignment. Instead it sets the first line flush left and the last line flush right**, with any middle lines centered. Lines are separated by \\. The number for the whole formula appears on the last line (or the first, with the leqno option). Because multline gives one number to one formula, you must **not blank out individual lines with \notag**. To push a middle line to one side, use \shoveleft{…} or \shoveright{…}, passing the entire line (but not the following \\) as the argument. The unnumbered form is multline*.

latex
\begin{multline}
  a + b + c + d + e + f \\
  + g + h + i + j + k + l + m + n
\end{multline}

This outputs the long sum broken across two lines, with the first part a + … + f flush left and the continuation + g + … + n flush right, and a single number for the whole formula. Any middle line is centered. multline takes no alignment & — you only mark the break points with \\.

When you want the broken lines 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. You place it inside a numbered structure such as equation (or align/gather), which supplies a single number for the whole. split cannot be used inside multline.

latex
\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}

This outputs one formula broken across two lines with the equals signs of both lines aligned, and the single number supplied by equation (centered vertically by default). split is the standard tool for aligning the lines of one broken formula at the equals sign, the counterpart of multline (which does not align). The body of a split should fill the whole of the enclosing structure; only commands that produce no output, such as \label, may sit alongside it.

EnvironmentFormulas / numbersAlignmentMain use
equationOne / oneNoA single displayed line
alignMany / per lineYes, at &Stack and line up at relations
gatherMany / per lineNo (each centered)Stack centered, no alignment
multlineOne / oneNo (left→right)Break a long formula, unaligned
splitOne / none (host supplies)One & columnAlign the lines of one formula

Piecewise definitions — cases

To define a function by cases, use the **cases environment**. A large left brace { is added automatically; within each row a **&** separates the “value” from the “condition,” and rows are separated by \\. The words of a condition (“if …,” and so on) would come out slanted and tightly spaced in math mode, so wrap them in \text{…} to set them upright and correctly spaced. cases itself carries no number, so it is usually placed inside equation to give the whole one number.

latex
\begin{equation}
  |x| =
  \begin{cases}
    x  & \text{if } x \ge 0, \\
    -x & \text{if } x < 0.
  \end{cases}
\end{equation}

This outputs a large left brace with two rows to its right — x (condition “if x ≥ 0”) and −x (condition “if x < 0”) — and a single number from equation. The value column and the condition column are each set flush left. Note that cases sets its contents in text style (the compact size); to render fractions large, use the **dcases** environment from the mathtools package, which sets them in display style.

Controlling numbers — \notag, \tag, subequations

In the numbered environments such as align and gather, each line is numbered automatically. amsmath provides ways to adjust this line by line.

  • Drop the number on one line: put \notag (or the synonym \nonumber) before that line’s \\. \notag cannot be used outside a display environment.
  • Set or override a number yourself: \tag{…} typesets whatever you give as the number, in place of the automatic one (parentheses are added for you). \tag*{…} adds no parentheses and prints your text literally.
  • Group related formulas as 1a, 1b, …: wrapping them in the subequations environment numbers the inner formulas (1a), (1b), … — the parent number plus a letter.
latex
\begin{align}
  y &= (x + 1)^2           \notag \\
    &= x^2 + 2x + 1        \\
  E &= mc^2                \tag{$\star$}
\end{align}

In this output the first line is unnumbered (thanks to \notag), the second carries its automatic number as usual, and the third gets (⋆) in place of the automatic number. Combining \notag and \tag lets you leave the intermediate lines of a derivation unnumbered and number only the concluding line.

To collect related formulas under one number, wrap them in **subequations**. If the preceding numbered formula was (4), the formulas in the group become (4a), (4b), (4c), …. A \label placed immediately after \begin{subequations} refers to the parent number (4), not the child (4a).

latex
\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}

This outputs the two formulas aligned at the equals sign and numbered (4a) and (4b) — the parent number plus a letter (the parent number being the running count in your document). \eqref{eq:gauss} then yields (4a), while \eqref{eq:max} yields (4), standing for the whole group.

Referring back — \label and \eqref

Tag a numbered formula with \label{…} and you can cite it by number from the text. \ref{…} returns the number itself (e.g. 3.2), while amsmath’s **\eqref{…} adds the parentheses** and returns (3.2). The parentheses from \eqref are always set upright regardless of the surrounding font, which makes \eqref the convenient choice for equation references. In a multi-line environment, put \label on the line whose number you want (per line in align; per child formula in subequations).

latex
式~\eqref{eq:euler} より $e^{i\pi} = -1$ が従う。

By~\eqref{eq:euler}, we have $e^{i\pi} = -1$.

This outputs, for instance, “By equation (1), …” — the number of the formula labelled eq:euler inserted in parentheses into the running text. To resolve references you normally compile twice, as cross-references require. The general mechanics of \ref/\label are covered on the “Cross-references” page. To reset numbering per chapter or section (so you get (1.1), (2.1), …), write \numberwithin{equation}{section} in the preamble.

Avoid eqnarray

Older documents sometimes use **eqnarray** (and eqnarray*) for multi-line math. This environment predates amsmath, but today you should not use it. Its worst flaw is the spacing around relations: eqnarray inserts unnaturally wide space on each side of an equals sign, and that spacing does not match the other math environments. Worse, with long lines the equation number can overprint the body text, and \qedhere from theorem packages does not work inside it.

Both the official amsmath manual and the LaTeX reference recommend avoiding eqnarray in favour of **align (to line up several formulas) or equation + split** (to break a single formula). Mind the difference in where the & goes, too: the align family puts & before the relation, whereas eqnarray was designed to put it after — part of why its spacing goes wrong. For new work, reach for the amsmath environments without hesitation.