Piecewise Functions in LaTeX: the cases Environment
Quick answer
- Package:
\usepackage{amsmath}, then\begin{cases} ... \end{cases}inside math mode. - Rows: value
&condition, rows separated by\\. Words go in\text{if }and\text{otherwise}. - Full-size fractions:
dcasesfrommathtools. Brace on the right:rcases.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
|x| =
\begin{cases}
x & \text{if } x \ge 0, \\
-x & \text{if } x < 0.
\end{cases}
\]
\end{document}A piecewise function is defined by different formulas on different parts of its domain, and in LaTeX it is written with the cases environment from amsmath. It draws the large left brace, aligns the values and the conditions in two columns, and works inline or in display math. This guide covers the basic syntax, text in the conditions, display-size rows with dcases, braces on the right, numbering, ready-made examples and the errors that come up most. Every snippet compiles as shown.
Piecewise Functions with \begin{cases}
Inside cases, every row is one piece of the function. An ampersand & separates the value from its condition, and \\ ends the row (not needed after the last one). The environment must be inside math mode, usually a displayed equation:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
The Heaviside step function is
\[
H(x) =
\begin{cases}
0 & \text{if } x < 0, \\
1 & \text{if } x \ge 0.
\end{cases}
\]
With three pieces:
\[
f(x) =
\begin{cases}
x^2 & \text{for } x < 0, \\
0 & \text{for } 0 \le x \le 1, \\
\ln x & \text{for } x > 1.
\end{cases}
\]
\end{document}Both columns are left-aligned, and the brace grows with the number of rows. The same code works inline, $f(x) = \begin{cases} ... \end{cases}$, but a tall brace in running text pushes the lines apart; display math reads better. How display math works is covered in inline and display math.
Text in cases: \text{if} and Otherwise
Conditions are still math, so words must go in \text{...}. Without it, “if” is set as the italic variables i and f and the spaces disappear. Put the space inside the braces: \text{if } x > 0. A catch-all row is written with \text{otherwise}:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\operatorname{sgn}(x) =
\begin{cases}
-1 & \text{if } x < 0, \\
0 & \text{if } x = 0, \\
1 & \text{otherwise}.
\end{cases}
\]
\[
\mathbf{1}_A(x) =
\begin{cases}
1 & x \in A, \\
0 & x \notin A.
\end{cases}
\]
\end{document}Whether you write “if”, “for” or only the condition, and whether the rows end with a comma, is a matter of style; just keep it consistent. For named functions such as \operatorname{sgn}, see math functions in LaTeX.
Display-Style Cases with dcases (mathtools)
cases typesets its rows in text style, the size of inline math, so fractions, sums and integrals come out small. The dcases environment from mathtools uses display style instead, and dcases* additionally sets the condition column as normal text, so no \text is needed:
\documentclass{article}
\usepackage{mathtools} % loads amsmath
\begin{document}
\[
g(x) =
\begin{cases}
\frac{1}{x} & x \neq 0 \\
0 & x = 0
\end{cases}
\qquad
g(x) =
\begin{dcases}
\frac{1}{x} & x \neq 0 \\
0 & x = 0
\end{dcases}
\]
\[
h(n) =
\begin{dcases*}
\sum_{k=1}^{n} k & if $n$ is positive, \\
0 & otherwise.
\end{dcases*}
\]
\end{document}For a single large fraction in an ordinary cases, \dfrac does the same job. mathtools also provides cases*, the text-condition version of plain cases.
Left Brace vs Right Brace
The brace of cases is always on the left. For a brace on the right, for instance to label a group of equations, use rcases from mathtools, or build it yourself from an array with \left., an invisible delimiter, on the other side:
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\[
\begin{rcases}
x + y = 3 \\
x - y = 1
\end{rcases}
\implies x = 2,\ y = 1
\]
\[
\left.
\begin{array}{l}
a > 0 \\
b > 0
\end{array}
\right\}
\implies ab > 0
\]
\[
f(x) = \left\{
\begin{array}{ll}
x^2 & \text{if } x \ge 0 \\
-x & \text{if } x < 0
\end{array}
\right.
\]
\end{document}The last example is the manual version of cases, which you still see in older documents. Every \left needs a matching \right;\right. closes it without drawing anything. More delimiters are listed in brackets in LaTeX.
Turn a photo of a formula into LaTeX
Have a piecewise function on paper or in a PDF? The free Math to LaTeX tool converts an image or a typed description into a cases environment you can paste straight into your document.
Aligning Conditions and Several Piecewise Functions
Within one cases the conditions already start in the same column. To line up the relation symbols inside the conditions as well, use an array with separate columns for each side. To align several piecewise definitions at their equals signs, put each cases in a line of align:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
f(x) = \left\{
\begin{array}{@{}l@{\quad}r@{}l@{}}
x^2, & x & {} < 0 \\
2x, & 0 \le x & {} \le 10 \\
100, & x & {} > 10
\end{array}
\right.
\]
\begin{align}
u(x) &= \begin{cases} 1 & x \ge 0 \\ 0 & x < 0 \end{cases} \\
\operatorname{ReLU}(x) &= \begin{cases} x & x \ge 0 \\ 0 & x < 0 \end{cases}
\end{align}
\end{document}The multiline equations guide covers align and its relatives in detail.
Numbering a Piecewise Function
Inside an equation environment the whole piecewise function gets one number. To number every case on its own, use numcases from the cases package. Its first argument is the left-hand side, and its second column is text, so math there needs $...$:
\documentclass{article}
\usepackage{amsmath}
\usepackage{cases}
\begin{document}
One number for the whole function:
\begin{equation}
f(x) =
\begin{cases}
x^2 & \text{if } x \ge 0 \\
-x & \text{if } x < 0
\end{cases}
\label{eq:f}
\end{equation}
One number per case:
\begin{numcases}{g(x) =}
x^2, & $x \ge 0$ \label{eq:g-pos} \\
-x, & $x < 0$ \label{eq:g-neg}
\end{numcases}
Equation \eqref{eq:f}, and the cases \eqref{eq:g-pos} and \eqref{eq:g-neg}.
\end{document}Labels and references for equations are explained in equation numbering.
Piecewise Function Examples
| Function | LaTeX |
|---|---|
| Absolute value | |x| = \begin{cases} x & x \ge 0 \\ -x & x < 0 \end{cases} |
| Sign function | \operatorname{sgn}(x) = \begin{cases} -1 & x < 0 \\ 0 & x = 0 \\ 1 & x > 0 \end{cases} |
| Heaviside step | H(x) = \begin{cases} 0 & x < 0 \\ 1 & x \ge 0 \end{cases} |
| ReLU | \operatorname{ReLU}(x) = \begin{cases} x & x > 0 \\ 0 & \text{otherwise} \end{cases} |
| Kronecker delta | \delta_{ij} = \begin{cases} 1 & i = j \\ 0 & i \neq j \end{cases} |
| Parity | (-1)^n = \begin{cases} 1 & n \text{ even} \\ -1 & n \text{ odd} \end{cases} |
Common Errors and Fixes
| Message or symptom | Cause | Fix |
|---|---|---|
| Missing \right. inserted | A manual \left\{ without a closing \right, or \left and \right split across lines of align | Close with \right. in the same line, or use cases, which needs neither |
| Missing $ inserted | cases used in normal text, outside math mode | Put it inside \[ ... \], equation or $...$ |
| Misplaced alignment tab character & | amsmath not loaded, so \begin{cases} falls back to an old plain TeX macro | \usepackage{amsmath} |
| LaTeX Error: Environment dcases undefined | dcases, rcases and cases* come from mathtools | \usepackage{mathtools} |
| Extra alignment tab has been changed to \cr | More than one & in a row; cases has only two columns | Keep one & per row, or use an array with more columns |
| “ifx>0” in italics, no spaces | Words written as math | \text{if } x > 0, or dcases* for text conditions |
| Fractions and sums look too small | cases uses text style | dcases from mathtools, or \dfrac |
Frequently Asked Questions
How do I write a piecewise function in LaTeX?
Load amsmath and use the cases environment inside math mode: f(x) = \begin{cases} x^2 & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}. Each row is one case, & separates the value from the condition, and \\ starts the next row. The large left brace is added automatically.
Which package do I need for cases in LaTeX?
amsmath, loaded with \usepackage{amsmath}. It defines the cases environment. The mathtools package, which loads amsmath for you, adds dcases (display-style rows), dcases* (text conditions) and rcases (a brace on the right).
How do I write “if” and “otherwise” in a piecewise function?
Wrap the words in \text{...}, which typesets them as upright text with normal spaces: \text{if } x > 0 and \text{otherwise}. Without \text, LaTeX treats the letters as variables, sets them in italic and drops the spaces, so “if x” turns into “ifx”.
Why are the fractions in my cases environment so small?
cases sets its rows in text style, the size used for inline math, so \frac and \sum come out small. Use dcases from the mathtools package to get full display-size rows, or write \dfrac for a single fraction.
How do I put the brace on the right side?
With mathtools, use the rcases environment, which works like cases with the brace on the right. Without mathtools, build it with an array and an invisible left delimiter: \left. \begin{array}{ll} ... \end{array} \right\}.
How do I number each case of a piecewise function?
Load the cases package and use numcases: \begin{numcases}{f(x)=} x^2, & $x \ge 0$ \\ -x, & $x < 0$ \end{numcases}. Every row gets its own equation number and can carry a \label. The second column of numcases is in text mode, so math there needs dollar signs.
Next Steps
- Multiline Equations –
align,gatherandsplit - Brackets in LaTeX – Braces,
\leftand\rightand sizing - Matrices in LaTeX – The other two-dimensional math layout
- Equation Numbering – Labels, references and custom numbers
- Math Functions –
\sin,\logand\operatorname - Learn LaTeX: Tutorial for Beginners – The basics every document builds on
Free LaTeX Math Tools
- Math to LaTeX – Convert a formula image into LaTeX code
- Equation to LaTeX – Describe an equation and get the code
- LaTeX Preview – Check a
casesenvironment instantly - LaTeX Symbols Reference – Relations, arrows and operators for the conditions
Write math faster with AI
Describe the function in words and Octree's AI writes the cases environment, loads the right packages and fixes the errors, with the PDF compiled next to your code.