Tx · LaTeX & TikZ

How to Write Two Right-Hand Sides in a LaTeX Equation

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the layout

    Use aligned lines for successive equalities, or cases when the right-hand side depends on a condition.

  2. 2. Load amsmath

    Add the amsmath package to your document preamble.

  3. 3. Align at equals

    In align, put an ampersand before each equals sign and use a line break between rows.

  4. 4. Choose numbering

    Use align for numbered rows, align* for unnumbered rows, or aligned inside equation for one number.

Advertisement

If you want to show one expression equal to different forms on separate lines, the usual solution is the align environment from the amsmath package. Put & before each equality sign to align the equations, and use \\ to start a new row.

If you mean that a quantity has one right-hand side in one situation and a different right-hand side in another, use a cases environment instead. These two layouts express different ideas, so choose the one that matches the mathematics.

Aligning a chain of equalities

Here is a complete minimal document. It uses align* to show a multi-line derivation without equation numbers:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align*}
f(x) &= x^2 + 1 \\
     &= (x - 1)(x + 1) + 2
\end{align*}

\end{document}

The & marks the alignment point. In this example, each & comes just before =, so the equals signs line up vertically. The second row has spaces in the source only for readability; LaTeX’s alignment marker, not those spaces, controls the layout. The \\ ends the first row.

Use align instead of align* if you want LaTeX to number the rows. For a derivation that should have just one equation number, put an aligned environment inside equation:

\begin{equation}
\begin{aligned}
f(x) &= x^2 + 1 \\
     &= (x - 1)(x + 1) + 2
\end{aligned}
\end{equation}

In this version, the entire aligned block gets one equation number. The aligned environment is designed to be used inside another math environment, such as equation or \[...\].

When the right-hand side depends on a condition

If you are defining a function that takes different values under different conditions, a chain of equalities is not the right notation. Use cases:

\[
f(x) =
\begin{cases}
x^2, & x \geq 0, \\
-x,  & x < 0.
\end{cases}
\]

The first column gives the possible values of the function; the second gives the conditions. The & separates those columns. The \\ starts the next case. The braces and spacing are supplied by cases automatically.

Common mistakes

  • Using \\ in ordinary math mode. A line break needs a suitable multi-line environment, such as align, aligned, or cases.
  • Forgetting the alignment marker. Without &, LaTeX has no specified point at which to line up the rows. Place it before the symbol you want aligned.
  • Using align inside equation. Both environments manage displayed math, so nesting them directly causes an error. Use aligned inside equation when you want a single number.
  • Using cases for a derivation. cases means the result depends on conditions; it is not simply a way to break a long equation across lines.
  • Writing two different values as simultaneous equalities. If you write x = a and x = b without conditions, you are asserting that both equalities hold, which requires a = b. Use separate cases if only one equality holds at a time.

For a short chain on one line, no special environment is needed: write $a=b=c$. Use align when the expressions should be split across rows or aligned with other equations.

References

  1. The amsmath package documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.