Piecewise functions in LaTeX (cases)

The cases environment from amsmath writes a function that is defined by cases: a large left brace and one case per line, with & between the value and its condition.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{mathtools}

Function with two cases

Each line has the form value, &, condition, \\. Both columns are left-aligned and in math mode, so words in the condition go into \text{...}; keep the space inside its braces.

\[
  |x| =
  \begin{cases}
    x  & \text{if } x \ge 0 \\
    -x & \text{if } x < 0
  \end{cases}
\]
Change the formula

The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.

∣x∣={xif x≥0−xif x<0|x| = \begin{cases} x & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}

Open this formula in the editor

Cases with fractions

cases sets its entries in text size, so fractions and sums come out small. dcases from mathtools sets every entry in display size. mathtools loads amsmath itself.

\[
  f(x) =
  \begin{dcases}
    \frac{\sin x}{x} & \text{if } x \neq 0 \\
    1                & \text{otherwise}
  \end{dcases}
\]
Change the formula

The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.

f(x)={sin⁡xxif x≠01otherwisef(x) = \begin{dcases} \frac{\sin x}{x} & \text{if } x \neq 0 \\ 1 & \text{otherwise} \end{dcases}

Open this formula in the editor

Conditions as normal text

In the starred versions cases* and dcases* from mathtools the second column is normal text. \text is no longer needed there, and a formula in the condition goes between $...$.

\[
  \operatorname{sgn}(x) =
  \begin{cases*}
    1  & if $x > 0$ \\
    0  & if $x = 0$ \\
    -1 & otherwise
  \end{cases*}
\]
Result
The compiled example

Open in the editor

Brace on the right

rcases from mathtools puts the brace on the right, for example to draw one conclusion from several lines.

\[
  \begin{rcases}
    x + y = 3 \\
    x - y = 1
  \end{rcases}
  \implies x = 2
\]
Change the formula

The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.

x+y=3x−y=1}  ⟹  x=2\begin{rcases} x + y = 3 \\ x - y = 1 \end{rcases} \implies x = 2

Open this formula in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages