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. Put the expression, then &, then the condition (usually with \text{...}), and end each line with \\. LaTeX draws the left brace automatically.

Example

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

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?

cases is unnumbered as a block. Put it inside an equation environment to number the whole thing once, or add \tag{...} for a custom label.

Try the fix in LetX

Open the editor, paste your code, and compile in 1 second to see the error clear — free, in your browser.

Open LetX Free