Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 38 ยท Math Typesetting

Aligning and Numbering Equations

Align multi-line equations with align, aligned, gather, split and multline, control equation numbers with \tag, \nonumber and \label, and avoid the common amsmath mistakes.

Real derivations are rarely one line. You step from one expression to the next, and the equals signs should line up so the reader can follow the logic. amsmath provides a family of environments for this. Choosing the right one is mostly a matter of asking two questions: where do the lines align, and which lines get numbers?

Load the package first:

LATEX
\usepackage{amsmath}

Never use the old eqnarray environment. It has bad spacing and is replaced by align.

align: line up at the & marks

In align, each line is one equation. A & marks the point that lines up vertically, usually just before the =. Lines end with \\.

LATEX
\begin{align}
  (a+b)^2 &= (a+b)(a+b)   \\
          &= a^2 + 2ab + b^2
\end{align}

By default every line gets its own number: (1) and (2). Each & is an alignment point.

Several columns. align can hold more than one equation per row. Alternate & marks: the first marks the alignment, the second separates the next column pair:

LATEX
\begin{align}
  x &= 1 &  y &= 2 \\
  z &= 3 &  w &= 4
\end{align}

Reading the source: & before = aligns the equals sign inside a pair, and the & between the pairs starts the next pair.

Controlling numbers

Command or form Effect
align*, equation*, gather* No numbers on any line
\nonumber or \notag at the end of a line Suppress the number for that line only
\tag{A} Replace the number with your own text ((A))
\tag*{A} The same without parentheses
\label{eq:x} Name the line so \eqref{eq:x} can refer to it
LATEX
\begin{align}
  f(x) &= (x+1)^2 \label{eq:square} \\
       &= x^2 + 2x + 1 \nonumber \\
       &= (x+1)(x+1) \tag{expanded}
\end{align}
As Equation~\eqref{eq:square} shows, \ldots

\eqref adds the parentheses around the number for you. Put \label after the equation content on the line it names. Cross-references get a full treatment in chapter 5.

Equation numbers can include the section number: \numberwithin{equation}{section} in the preamble gives (2.1), (2.2) and so on. It is a good habit for longer documents.

aligned: a block inside one equation

The align environment is for whole displays. When you need an aligned block as part of a larger formula (to sit next to another object, such as a brace or a number), use aligned, which has the same syntax but is placed inside a math mode.

LATEX
\begin{equation}
  \left\{ \begin{aligned}
    x + y &= 3 \\
    x - y &= 1
  \end{aligned} \right.
\end{equation}

One number for the entire block. Variants gathered and alignedat also exist.

gather: centered lines

When equations should be centered rather than aligned, use gather:

LATEX
\begin{gather}
  a^2 + b^2 = c^2 \\
  e^{i\pi} + 1 = 0
\end{gather}

split and multline for one long equation

A single equation that is too long for one line should be broken with split (aligned at &, one number) or multline (first line left, last line right, middle lines centered).

LATEX
\begin{equation}
  \begin{split}
    f(x) &= (x+1)^3 \\
         &= x^3 + 3x^2 + 3x + 1
  \end{split}
\end{equation}

\begin{multline}
  a + b + c + d + e + f + g + h + i + j + k \\
  + l + m + n + o + p + q + r + s + t
\end{multline}

Neither lines will be broken for you. LaTeX does not break display formulas automatically, so you choose the break points, preferably before a +, - or = sign and not inside brackets.

Text between equation lines

Use \intertext{...} to add words between two aligned lines without breaking the alignment:

LATEX
\begin{align}
  a &= b + c \\
\intertext{and therefore}
  d &= e + f
\end{align}

Putting a normal paragraph between two align blocks also works, but the alignment points are lost, which is sometimes what you want.

Choosing the right environment

flowchart TD
  Q{"How many lines?"} -->|"One"| E["equation (numbered) or the unnumbered display form"]
  Q -->|"Several, align at ="| A["align"]
  Q -->|"Several, no alignment"| G["gather"]
  Q -->|"One equation, too long"| L["split inside equation, or multline"]
  Q -->|"A block next to something else"| AL["aligned inside equation"]

Rule of thumb: align for derivations, equation for one numbered line.

Try it

The box below renders the whole text as one display equation, so you can edit an align block and see the alignment. KaTeX supports align, aligned, gather, split, \tag and \nonumber; it does not support multline. The presets are in the choice list.

Aligned and numbered equations (KaTeX)

Common mistakes

  • Using align for one line: equation or \[ \] is enough.
  • Missing &: lines do not align, and LaTeX raises no error because it is legal.
  • Putting a blank line inside an align block: this is an error ("Missing $ inserted" or a runaway argument). Equations cannot contain blank lines.
  • Leaving a trailing \\ after the last line: an empty numbered line appears.
  • Using \\ inside a plain equation: it needs split or aligned.
  • Mixing eqnarray with amsmath code: replace it with align.
  • Placing \label before the equation starts: it must be inside the numbered environment.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which environment lines up several equations at their equals signs, one number per line?

    align uses & to mark alignment points and numbers each line.
  2. 2. How do you suppress the number of a single line inside align?

    \nonumber turns off the number for just that line; align* turns off all numbers.
  3. 3. What does \eqref{eq:x} print?

    \eqref wraps the referenced number in parentheses automatically.
  4. 4. Which environment is meant for one long equation split into aligned parts with a single number?

    split lives inside equation or align and shares one number across its lines.

Summary

  • Use align for multi-line derivations (aligned at &), gather for centered lines, split and multline for one long equation, aligned for a block inside another formula.
  • Starred versions remove numbers; \nonumber, \tag and \label control single lines.
  • \intertext inserts words without losing alignment.
  • LaTeX never breaks display formulas for you: choose the break points.
  • Replace eqnarray with align.

Next: Lists in LaTeX