How-to

How to Write a Piecewise Function in LaTeX

The fix

To write a piecewise function in LaTeX, load \usepackage{amsmath} and use the cases environment inside math mode. On each row write the value, then &, then the condition (put words in \text{...}), and end every row except the last with \\. LaTeX draws the left brace for you.

Not sure it worked on your document? Paste it into LetX and compile it, free and in the browser.

Why it happens

cases is an amsmath environment: a two-column array with a brace on the left, values in the first column and conditions in the second, separated by &. Without amsmath, \begin{cases} falls back to the older plain TeX \cases macro, which expects a different syntax. So the first symptom of a missing \usepackage{amsmath} is "Misplaced alignment tab character &", not a missing-environment error.

Example

\usepackage{amsmath}
% in the document:
\[
f(x) =
\begin{cases}
  x^2 & \text{if } x \ge 0 \\
  -x  & \text{if } x < 0
\end{cases}
\]

Conditions, commas and "otherwise"

Everything inside cases is math, so a word typed directly comes out as italic variables with its spaces removed: "if x" becomes "ifx". Put words in \text{...} and keep the space inside the braces, as in \text{if } x > 0. \text{otherwise} is the usual last row.

Style guides differ on whether the value ends with a comma and whether the condition starts with "if". Pick one form and use it throughout. For a wider gap between value and condition, add \quad after the &.

\usepackage{amsmath}
% in the document:
\[
|x| =
\begin{cases}
  x,  & \quad x \ge 0, \\
  -x, & \quad \text{otherwise}.
\end{cases}
\]

Full-size fractions: dcases from mathtools

cases sets its rows in text style, so a \frac inside it is as small as in inline math. The mathtools package (which loads amsmath) adds dcases, which sets the rows in display style.

dcases* goes one step further: its second column is ordinary text, so conditions need no \text{...}, and any math in them goes in $...$. mathtools also adds rcases, a brace on the right that groups several lines into one conclusion.

\usepackage{mathtools}
% in the document:
\[
g(x) =
\begin{dcases}
  \frac{1}{x} & x \neq 0 \\
  0           & \text{otherwise}
\end{dcases}
\qquad
h(x) =
\begin{dcases*}
  \frac{\sin x}{x} & when $x \neq 0$ \\
  1                & otherwise
\end{dcases*}
\]

Numbering a piecewise function

cases is not numbered on its own. Inside \begin{equation} ... \end{equation} the whole function gets one number, which you can \label and cite with \eqref.

To number each row, load the cases package and use numcases. Its argument is the left-hand side, and its second column is in text mode, so conditions are plain words with any math in $...$. Each row can carry its own \label.

cases also works inside align when the function is one step of a longer derivation. Put the alignment & before \begin{cases}; the & inside belong to cases.

\usepackage{amsmath}
\usepackage{cases}
% in the document:
\begin{numcases}{f(x)=}
  x^2 & if $x \ge 0$ \label{eq:pos} \\
  -x  & if $x < 0$  \label{eq:neg}
\end{numcases}
For $x \ge 0$, see \eqref{eq:pos}.

Errors and what they mean

"Misplaced alignment tab character &": amsmath is not loaded, or the & sits outside any alignment environment. Add \usepackage{amsmath}.

"Missing $ inserted": cases was used outside math mode. Wrap it in \[ ... \], an equation environment, or $...$ for inline use.

"Extra alignment tab has been changed to \cr": a row has more than one &. cases has exactly two columns; for more, use an array inside \left\{ ... \right.

Conditions in italics with the spaces gone: the words are not inside \text{...}.

Frequently asked questions

Why are my conditions in italics?

Text in math mode is italic and drops spaces. Wrap words in \text{...} (from amsmath) so a condition like "if x >= 0" reads correctly.

How do I number a piecewise function?

Put cases inside an equation environment to number the whole function once. To number every row, use numcases from the cases package.

How do I write "otherwise" in a piecewise function?

Use \text{otherwise} as the condition on the last row, after the &. Without \text the word is set as nine italic variables.

How do I make fractions full size inside cases?

Use dcases from the mathtools package. It sets each row in display style, so \frac is no longer shrunk.

Can I put the brace on the right?

Yes, with rcases from mathtools. It groups the rows with a right-hand brace, usually followed by a conclusion such as \Rightarrow x = 1.

Does cases work inside align?

Yes. Write the alignment & before \begin{cases}, as in f(x) &= \begin{cases} ... \end{cases}. The & inside cases belong to cases itself.

Related LaTeX fixes

Browse all LaTeX problems & fixes, or start with the LaTeX guide.

Learn the topic properly

Try the fix in LetX

Open the editor, paste your code, and compile in seconds to see the error clear. Free, in your browser.

Open LetX Free