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.

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

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.

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