Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
Who this is for: researchers and analysts typesetting a piecewise definition, a conditional formula or a case-by-case result in LaTeX. After reading, you can write a working cases block, put the conditions in text, and switch to dcases or rcases when the default layout is wrong.
In LaTeX, a piecewise definition is typeset with the cases environment from the amsmath package. Each row has a value, an & alignment mark and a condition, and rows end with \\. The environment draws a large left brace around the rows.
What it is used for in research
The amsmath User’s Guide describes the construction directly: “cases” constructions are common in mathematics, and the package provides a cases environment to make them easy to write. Typical uses are piecewise-defined functions, indicator or loss functions, thresholded rules in a methods section, and parity or boundary conditions in a derivation. The searches latex cases math and cases latex math describe the same task.
Install or access (official source only)
The cases environment is part of amsmath, maintained by the LaTeX Project team (originally with the American Mathematical Society) and included in the standard TeX distributions, for example MacTeX on macOS. Load it in the preamble:
\usepackage{amsmath}
The extended variants (dcases, rcases and others, covered below) come from the mathtools package. Its CTAN page lists version 1.31 (2024-10-04) under the LaTeX Project Public License 1.3c; it requires mhsetup and builds on amsmath. The amsmath guide notes that loading mathtools loads amsmath automatically, so one line is enough:
\usepackage{mathtools}
Step-by-step: write a piecewise function
- Add
\usepackage{amsmath}(ormathtools) to the preamble. - Open a math environment. The amsmath guide states that
casesmust appear within an enclosing math environment: inline between$...$or in any display environment such asequation. - Write
\begin{cases}, then one row per case: the value,&, then the condition. - End every row except the last with
\\, then close with\end{cases}. - Wrap each condition in
\text{...}when it contains words such as “if” or “otherwise”, and use nested$...$insidetextfor math in the condition.
The example below is the one given in the amsmath User’s Guide, wrapped in an equation environment:
\begin{equation}
P_{r-j}=\begin{cases}
0 & \text{if $r-j$ is odd}, \\
r!\,(-1)^{(r-j)/2} & \text{if $r-j$ is even}.
\end{cases}
\end{equation}
The guide’s note on this example: it uses text for the conditions, with nested math formulas where needed.
Output and how to read it
The result is one large left brace followed by two aligned columns: values on the left, conditions on the right, one case per line. The amsmath guide notes that cases is set in text style (textstyle), so fractions, sums and integrals inside it appear in their compact form. If display style is wanted, it must be requested explicitly, and mathtools provides dcases for this purpose.
Choosing among cases, dcases, rcases and the starred forms
The mathtools manual documents these variants. The rows below come from its syntax table and descriptions.
| Environment | Package | Columns | What changes |
|---|---|---|---|
cases |
amsmath | math & math | Brace on the left; rows in text style. |
dcases |
mathtools | math & math | Same output as cases except the rows are set in display style. |
dcases* |
mathtools | math & text | The second column is set in the normal roman font of the document, so conditions need no text. |
cases* |
mathtools | math & text | Text second column, with rows in text style. |
rcases, rcases* |
mathtools | as cases, cases* |
The brace is placed on the right-hand side. |
drcases, drcases* |
mathtools | as dcases, dcases* |
Display style with the brace on the right. |
Display-style rows with dcases
The mathtools manual’s own example sets a physical relation and an integral in a dcases block, with the second column holding text such as “Integral is display style”. Use dcases when a row contains a fraction, sum or integral that looks cramped in text style.
Text conditions without text
With a starred form, the second column is already text, so a condition can be typed as plain words. Per the manual, dcases* uses the normal roman font of the document.
Brace on the right with rcases
rcases is similar to cases but with the brace on the right-hand side, as the amsmath guide and the mathtools manual both describe.
Troubleshooting
- Undefined control sequence or environment: the package is not loaded.
casesneedsamsmath;dcasesandrcasesneedmathtools. - Conditions print as italic run-together letters: words were typed in math mode. Wrap them in
\text{...}, or use a starred mathtools form. - Error when the environment is used on its own: the amsmath guide requires an enclosing math environment (
$...$or a display environment). - Old
\cases{...}syntax fails: the amsmath guide explains that once amsmath is loaded, the old forms ofpmatrix,matrixandcasescan no longer be used because of naming conflicts. Use\begin{cases}...\end{cases}. - Fractions look small: this is the text-style default described above; switch to
dcases.
Free alternatives (table)
These are the options documented in the sources above. The mathtools package is distributed through CTAN under LPPL 1.3c.
| Option | Provided by | Use it when |
|---|---|---|
cases |
amsmath | A standard piecewise block in text style is enough. |
dcases, dcases* |
mathtools | Rows contain large operators, or the conditions are plain text. |
rcases, drcases |
mathtools | The brace belongs on the right. |
For related LaTeX tasks on this site, see LaTeX math signs for symbol commands, aligning equations to the left for alignment, and the structure of a LaTeX document for preamble basics.
FAQ
How do I write a piecewise function in LaTeX?
Load amsmath, open a math environment, and use \begin{cases} value & \text{if condition} \\ value & \text{otherwise} \end{cases}. The last row needs no trailing \\.
Do I need a package for cases?
Yes. The cases environment is provided by amsmath.
Why are my fractions small inside cases?
The amsmath guide says cases is set in text style. Use dcases from mathtools for display style.
Can the brace go on the right?
Yes. Use rcases (or drcases for display style) from mathtools.
Sources (official docs)
- User’s Guide for the amsmath Package, version 2.1 (revised 2020-02-18), AMS and LaTeX Project: amsldoc.pdf, read 2026-11-07.
- The mathtools package manual, via CTAN: mathtools.pdf, read 2026-11-07.
- CTAN package page for mathtools: ctan.org/pkg/mathtools (version 1.31, 2024-10-04; LPPL 1.3c), read 2026-11-07.







