The cases
environment in LaTeX is widely used to typeset functions or equations with multiple conditions.
It produces a large curly bracket on one side and aligns expressions neatly with their respective conditions.
Beginners often struggle with alignment, text formatting, and the choice between mbox and text.
This tutorial explains the cases
environment in detail with syntax and practical code.
Case environment with amsmath
The simplest way to create cases
is with the amsmath
package.
The environment aligns expressions on the left and conditions on the right under a single curly brace.
\begin{cases} expression & condition \\ expression & condition \end{cases}
\begin{cases} ... \end{cases}
- This environment produces a left curly bracket with aligned rows. Each row has an expression on the left and its condition on the right.
&
- The ampersand separates the expression from the condition. Both parts are aligned in two columns automatically.
\\
- This ends one row and starts the next row inside the cases environment.
\documentclass{article} \usepackage{amsmath} \begin{document} The absolute value of $x$ is defined as \[ |x| = \begin{cases} x & \text{if $x > 0$} \\ 0 & \text{if $x = 0$} \\ -x & \text{if $x < 0$} \end{cases} \] \end{document}
Using mbox and text
Inside cases conditions must be typeset in text mode. LaTeX provides two options mbox
and text. Both display text inside math mode but behave slightly differently.
x & \mbox{if $x > 0$} \\ x & \text{if $x > 0$}
\mbox{...}
- This creates a box with unbreakable text inside math mode. Line breaks are not allowed inside mbox.
\text{...}
- This comes from amsmath and permits proper line breaking and font handling inside math mode. Recommended over mbox for clarity.
\documentclass{article} \usepackage{amsmath} \begin{document} \[ f(x) = \begin{cases} x & \mbox{if $x > 0$} \\ 0 & \text{if $x \leq 0$} \end{cases} \] \end{document}
Using array for cases
The array environment can also be used to mimic the cases structure. Here curly braces are manually added with left and right commands.
\left\{ \begin{array}{l l} expression & condition \\ expression & condition \end{array} \right.
\begin{array}{l l}
- This defines an array with two columns both left aligned. The first holds the expression and the second holds the condition.
\left\{ ... \right.
- This creates a curly brace only on one side of the array. The opposite side is left open with a dot.
\documentclass{article} \usepackage{amsmath} \begin{document} \[ S_\infty = \left\{ \begin{array}{l l} a & \text{if $|r| < 1$} \\ \infty & \text{if $|r| \geq 1$} \end{array} \right. \] \end{document}
Enhanced cases with mathtools
The mathtools
package extends amsmath
by providing dcases
and rcases
environments for improved display.
These give cleaner alignment and optional right braces.
\begin{dcases} expression & condition \\ expression & condition \end{dcases}
dcases
- This environment works like cases but formats conditions in text mode automatically. No need for extra text or mbox commands.
rcases
- This version places the curly brace on the right side instead of the left which is useful for certain notations.
\documentclass{article} \usepackage{mathtools} \begin{document} \[ f(x) = \begin{dcases} x & if $x > 0$ \\ 0 & if $x = 0$ \\ -x & if $x < 0$ \end{dcases} \] \end{document}
Best Practice
1. For general piecewise definitions always use the amsmath cases environment because it is standard and reliable.
2. Prefer text over mbox since it allows proper formatting and breaks.
4. When you need cleaner alignment or right side braces use mathtools
with dcases
and rcases
.
5. The array method works but is less elegant.