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.
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.
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*}
\]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.
Pitfalls
casesworks only inside math mode. In normal text it stops with Missing $ inserted.- A line can hold one
&. A second one stops with Extra alignment tab has been changed to \cr. - Inside
equationthe whole definition gets one number. For one number per case, thecasespackage has thenumcasesenvironment.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
amsmath - Package
mathtools - How-to Text in math mode in LaTeX
- How-to Systems of equations in LaTeX
- Error Extra alignment tab has been changed to \cr