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.

LaTeX
\[ |x| =
\begin{cases}
  x  & \text{if } x \ge 0, \\
  -x & \text{if } x < 0.
\end{cases} \]
OutputThe absolute value of x equals x if x is at least 0, and minus x if x is less than 0, with a left brace.

Needs \usepackage{amsmath} in the preamble.

Full document you can compile
example.tex
\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.

LaTeX
\[ \operatorname{sgn}(x) =
\begin{cases*}
  1  & if $x > 0$ \\
  0  & if $x = 0$ \\
  -1 & otherwise
\end{cases*} \]
OutputThe sign function: 1 if x is greater than 0, 0 if x equals 0, minus 1 otherwise, with the conditions written as text.

Needs \usepackage{mathtools} in the preamble.

Full document you can compile
example.tex
\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.

LaTeX
\[ 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} \]
OutputThe same function f of x equals 1 over x for x greater than 0 and 0 otherwise, first with small fractions in cases, then with full-size fractions in dcases.

Needs \usepackage{mathtools} in the preamble.

Full document you can compile
example.tex
\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.

LaTeX
\[ g(x) =
\begin{rcases}
  x^2 & x \ge 0 \\
  0   & x < 0
\end{rcases}
\text{ is continuous} \]
OutputTwo cases for g of x with a right brace followed by the words is continuous.

Needs \usepackage{mathtools} in the preamble.

Full document you can compile
example.tex
\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.

Text drafted with AI from the cited documentation; every example compiled with TeX Live 2026; checked by Cem Demir. See the editorial policy.