Piecewise Functions in LaTeX: the cases Environment

One brace, one row per branch, one condition column — the exact shape of a piecewise definition and the four ways it goes wrong.

A piecewise definition is a brace plus two columns — a value and a condition — not a matrix with a big brace drawn on the left. The cases environment supplies the brace and keeps both columns left-aligned; everything else is the ordinary row syntax from Align. Conditions that talk about sets need Set Notation, and a tall branch brings back the sizing problem from Fractions.

Which one to use

Situation Write this Because
Two or three branches
cases
One row per branch; brace automatic.
A condition on each line
& \text{if } x>0
The ampersand splits value from condition.
A catch-all branch
\text{otherwise}
Words go in \text, not maths italics.
An indicator function
\mathbf{1}_{\{x>0\}}
The subscript names the set.
Full size inline
cases
Inline cases shrink; move it to its own line — display maths fixes it.
A branch containing a fraction
\dfrac
Keeps that row from collapsing.

The commands

What Type this Renders as
Two branches The absolute value written out; & splits value from condition.
f(x)=\begin{cases} x & x \ge 0 \\ -x & x < 0 \end{cases}
f(x)=\begin{cases} x & x \ge 0 \\ -x & x < 0 \end{cases}
Three branches Rows simply stack; the brace grows to cover all of them.
\operatorname{sgn}(x)=\begin{cases} 1 & x > 0 \\ 0 & x = 0 \\ -1 & x < 0 \end{cases}
\operatorname{sgn}(x)=\begin{cases} 1 & x > 0 \\ 0 & x = 0 \\ -1 & x < 0 \end{cases}
With if / otherwise Words belong in \text{}, otherwise they print as a product of italic letters.
\begin{cases} 1 & \text{if } x \in A \\ 0 & \text{otherwise} \end{cases}
\begin{cases} 1 & \text{if } x \in A \\ 0 & \text{otherwise} \end{cases}
Comma after the value House styles differ; a comma reads as the natural pause before a condition.
y=\begin{cases} t^2, & 0 \le t < 1 \\ 2t-1, & t \ge 1 \end{cases}
y=\begin{cases} t^2, & 0 \le t < 1 \\ 2t-1, & t \ge 1 \end{cases}
Indicator function The one case where the function name itself is a bold digit.
\mathbf{1}_A(x)=\begin{cases} 1 & x \in A \\ 0 & x \notin A \end{cases}
\mathbf{1}_A(x)=\begin{cases} 1 & x \in A \\ 0 & x \notin A \end{cases}
Wide condition column Branch widths are independent; the value column is sized by the widest entry.
g=\begin{cases} x^2+1 & 0 \le x < 3 \\ 7 & x \ge 3 \end{cases}
g=\begin{cases} x^2+1 & 0 \le x < 3 \\ 7 & x \ge 3 \end{cases}
Case inside a fraction A logistic curve needs no cases at all, which is usually the better answer when a branch exists purely to switch sign.
\frac{1}{1+e^{-x}}
\frac{1}{1+e^{-x}}
Aligned block in a branch Legal but heavy; two levels of stacking is the point to consider splitting the definition.
\begin{cases} \begin{aligned} x &= 1 \\ y &= 2 \end{aligned} & t > 0 \end{cases}
\begin{cases} \begin{aligned} x &= 1 \\ y &= 2 \end{aligned} & t > 0 \end{cases}

Worked examples

A step function

H(x)=\begin{cases} 0 & x < 0 \\ 1 & x \ge 0 \end{cases}
H(x)=\begin{cases} 0 & x < 0 \\ 1 & x \ge 0 \end{cases}

The Heaviside function, and the reason the condition column matters: both rows share the same variable name.

Clamping to a range

f(x)=\begin{cases} 0 & x < 0 \\ x & 0 \le x \le 1 \\ 1 & x > 1 \end{cases}
f(x)=\begin{cases} 0 & x < 0 \\ x & 0 \le x \le 1 \\ 1 & x > 1 \end{cases}

Three branches covering the middle band. Conditions overlap at endpoints only where the value agrees there.

Modulus as a definition

|x|=\begin{cases} x & x \ge 0 \\ -x & x < 0 \end{cases}
|x|=\begin{cases} x & x \ge 0 \\ -x & x < 0 \end{cases}

The bar notation and its cases expansion side by side; transcribing from an image usually gives you one and asks for the other.

A recursive-looking branch

a_n=\begin{cases} 1 & n = 0 \\ 2a_{n-1} & n > 0 \end{cases}
a_n=\begin{cases} 1 & n = 0 \\ 2a_{n-1} & n > 0 \end{cases}

Subscripts inside the value column behave exactly as they do outside cases.

Common mistakes

  • Leaving out the &. Two tokens with no separator print as one multiplication in the value column and no condition ever appears.
  • Writing \text{if }x>0 without the trailing space inside \text{}, which glues the word to the inequality.
  • Using <= or >=. Those are programming operators; LaTeX wants \le and \ge, and the ASCII form silently prints an unexpected symbol.
  • Trying to add your own \left\{ in front of cases. The environment already supplies the brace, so you get two.

Questions about this notation

Does cases need amsmath?
The cases environment comes from amsmath in a LaTeX document, which most classes load automatically. KaTeX and MathJax implement it directly, so a pasted cases block renders in browsers with no package at all.
How do I add a second condition on the same line?
Write it as one braced condition: & 0 \le x < 3 covers a range in a single column. For genuinely unrelated conditions, split them with a comma inside \text{} rather than adding another ampersand, because a third column will shift everything.
Can the value column contain a fraction?
Yes, and the row grows to fit it. Because cases uses display-style stacking, deep fractions inside branches make the whole definition tall — \tfrac often reads better there.
How do I write an indicator function, or keep a piecewise definition at full size?
The value column of cases is set in text style, so fractions inside it shrink. dcases from the mathtools package is the same environment at display style. For an indicator function the two usual forms are \mathbf{1}_{A}(x) and \mathbb{1}_{A}(x); the plain digit 1 is also standard and never depends on a font providing a blackboard-bold one.

Keep reading

Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.