How to Write Piecewise Functions with the cases Environment in LaTeX
Piecewise functions are essential in mathematics for defining functions that behave differently over different intervals. LaTeX provides the cases environment inside amsmath to typeset these cleanly. Whether you're writing analysis proofs or signal processing formulas, mastering piecewise notation is crucial. Bibby AI makes writing piecewise functions effortless with real-time preview and intelligent autocompletion — a smoother experience than Overleaf for complex math.
Basic Piecewise Function with cases
Use the cases environment inside a math display to define a piecewise function. Each case is separated by \\ and uses & to align the condition:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
f(x) =
\begin{cases}
x^2 & \text{if } x \geq 0, \\
-x & \text{if } x < 0.
\end{cases}
\]
\end{document}Piecewise Function with Multiple Conditions
For functions with three or more branches, simply add more rows. Use \text{} for the condition descriptions:
\[
|x| =
\begin{cases}
x & \text{if } x > 0, \\
0 & \text{if } x = 0, \\
-x & \text{if } x < 0.
\end{cases}
\]Right-Side Brace with rcases
Use the rcases environment when you need the brace on the right side, commonly used to group conditions that lead to a single conclusion:
\usepackage{mathtools}
\[
\begin{rcases}
x > 0 \\
y > 0
\end{rcases}
\Rightarrow x + y > 0
\]💡 Tips
- •Always load amsmath (or mathtools which extends it) before using the cases environment.
- •Use \text{} from amsmath for words inside math mode — never leave plain text unprotected in math.
- •For numbered piecewise equations, wrap the cases inside an equation environment instead of \[ \].
- •Bibby AI's autocomplete suggests the full cases skeleton so you can focus on the math, not the syntax.
Try This in Bibby AI
Write LaTeX faster with AI auto-complete and instant compilation.
Start Writing Free