Piecewise Function in LaTeX
To write a piecewise function in LaTeX, load amsmath and use the cases environment. Each line is value & condition \\: the & separates the value from its condition, and LaTeX adds the left brace.
\[ |x| =
\begin{cases}
x & \text{if } x \ge 0, \\
-x & \text{if } x < 0.
\end{cases} \]Needs \usepackage{amsmath} in the preamble.
Full document you can compile
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[ |x| =
\begin{cases}
x & \text{if } x \ge 0, \\
-x & \text{if } x < 0.
\end{cases} \]
\end{document}How do you write the conditions as words?
Inside cases everything is math, so words need \text{…}: \text{if } x \ge 0. Keep the space inside the braces, since math mode drops normal spaces. The cases* environment from mathtools sets the second column as text, so you write if $x > 0$ instead.
\[ \operatorname{sgn}(x) =
\begin{cases*}
1 & if $x > 0$ \\
0 & if $x = 0$ \\
-1 & otherwise
\end{cases*} \]Needs \usepackage{mathtools} in the preamble.
Full document you can compile
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\[ \operatorname{sgn}(x) =
\begin{cases*}
1 & if $x > 0$ \\
0 & if $x = 0$ \\
-1 & otherwise
\end{cases*} \]
\end{document}Why are fractions small inside cases?
cases sets its entries in the compact inline style, so \frac shrinks. dcases from mathtools uses the display style and keeps fractions full size. mathtools loads amsmath for you.
\[ f(x) =
\begin{cases}
\frac{1}{x} & x > 0 \\
0 & x \le 0
\end{cases}
\qquad
f(x) =
\begin{dcases}
\frac{1}{x} & x > 0 \\
0 & x \le 0
\end{dcases} \]Needs \usepackage{mathtools} in the preamble.
Full document you can compile
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\[ f(x) =
\begin{cases}
\frac{1}{x} & x > 0 \\
0 & x \le 0
\end{cases}
\qquad
f(x) =
\begin{dcases}
\frac{1}{x} & x > 0 \\
0 & x \le 0
\end{dcases} \]
\end{document}How do you put the brace on the right?
rcases from mathtools draws the brace on the right, which is useful when several lines lead to one conclusion. drcases is its display-style version.
\[ g(x) =
\begin{rcases}
x^2 & x \ge 0 \\
0 & x < 0
\end{rcases}
\text{ is continuous} \]Needs \usepackage{mathtools} in the preamble.
Full document you can compile
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\[ g(x) =
\begin{rcases}
x^2 & x \ge 0 \\
0 & x < 0
\end{rcases}
\text{ is continuous} \]
\end{document}How do you number each line of a piecewise function?
cases is one formula, so it gets a single number in equation. To number each line, use numcases from the cases package (a different package with the same name as the environment).
How do you test a piecewise function before compiling?
Type it in the equation editor to see the brace as you go. Fractions inside cases are covered in the fractions guide, and lining up several equations in the align guide.
Sources
- User’s Guide for the amsmath Package (CTAN), read Nov 28, 2026
- mathtools package documentation (CTAN), read Nov 28, 2026
- cases package documentation (CTAN), read Nov 28, 2026
Text drafted with AI from the cited documentation; every example compiled with TeX Live 2026; checked by Cem Demir. See the editorial policy.