Getting Started
Text Formatting
Mathematical Expressions
Document Structure
Page Layout
Errors & Troubleshooting
Piecewise Functions in LaTeX with the cases Environment
The answer is cases from amsmath: one & per row separating value from condition, \\ between rows, brace drawn automatically. Here is the absolute value function, the canonical example:
The condition column: \text, not \mbox
Words like "if" and "otherwise" are text, and they belong in \text{...} (also from amsmath). Note the trailing space inside \text{if } — math mode swallows the space otherwise and you get "ifx". Skip \mbox: it ignores the surrounding math style, so it sets the wrong size in sub- and superscripts.
Cramped fractions: dcases
cases sets each row in textstyle. Put a \frac in a branch and it comes out small and cramped, with the limits of sums and integrals squashed beside them instead of above. The fix is dcases from mathtools — identical syntax, displaystyle rows:
The blunt alternative is \dfrac inside a plain cases. It works for fractions and nothing else — sums, limits and integrals stay cramped — so once a document has more than one awkward branch, switch to dcases and be done. mathtools also gives you dcases*, which sets the condition column in upright text automatically, no \text needed.
Brace on the right: rcases
When several expressions collapse into one conclusion, the brace goes on the right. mathtools' rcases (and its displaystyle sibling drcases) handles it:
Numbering individual branches: numcases
Plain cases lives inside one equation and gets at most one number. If a proof needs to cite branch two on its own, use numcases from the cases package (confusingly named, unrelated to amsmath):
Note the shape: the left-hand side goes in the braces after \begin{numcases}, and the condition column is plain text mode — hence $x \ge 0$ with its own dollars, the opposite of the \text dance in amsmath cases.
Punctuation and style
- Comma between value and condition:
x, & \text{if } x \ge 0. House styles differ on whether the comma is required; being consistent is what matters. - An equation ending a sentence takes a period after the last branch — most journals copy-edit this in if you leave it out.
- Keep conditions parallel: all
\text{if } ...with a final\text{otherwise}, not a mix of "if", "for" and "when".
Nested cases: legal, ugly, avoid
A cases inside a cases compiles fine and reads terribly — the inner brace crowds the outer one and the condition columns stop lining up. Restructure instead: flatten the conditions into compound ones, or split the definition into two displayed equations.
Rule of thumb: if a piecewise definition needs nesting, the notation is doing a job that prose or a second function definition would do better.
Frequently asked questions
How do I write a piecewise function in LaTeX?
Use the cases environment from amsmath: f(x) = \begin{cases} x & \text{if } x \ge 0 \\ -x & \text{otherwise} \end{cases}. The & separates the value from its condition, \\ starts the next branch, and the left brace is drawn for you.
Why do fractions look tiny inside cases?
cases sets its rows in textstyle, so \frac shrinks. Load mathtools and use dcases instead of cases — same syntax, displaystyle rows. The blunt alternative is writing \dfrac in an ordinary cases, but dcases fixes integrals, sums and limits too, not just fractions.
How do I put the brace on the right side instead of the left?
mathtools provides rcases (and drcases for the displaystyle version): \begin{rcases} ... \end{rcases} draws the brace on the right, which is the convention when several expressions collapse into one conclusion.
Can each branch of a piecewise function get its own equation number?
Yes — the numcases environment from the cases package: \begin{numcases}{f(x)=} branch & condition \\ ... \end{numcases}. Each branch gets a number you can \label and reference individually, which plain cases cannot do.
Piecewise Functions in LaTeX with the cases Environment
The answer is cases from amsmath: one & per row separating value from condition, \\ between rows, brace drawn automatically. Here is the absolute value function, the canonical example:
The condition column: \text, not \mbox
Words like "if" and "otherwise" are text, and they belong in \text{...} (also from amsmath). Note the trailing space inside \text{if } — math mode swallows the space otherwise and you get "ifx". Skip \mbox: it ignores the surrounding math style, so it sets the wrong size in sub- and superscripts.
Cramped fractions: dcases
cases sets each row in textstyle. Put a \frac in a branch and it comes out small and cramped, with the limits of sums and integrals squashed beside them instead of above. The fix is dcases from mathtools — identical syntax, displaystyle rows:
The blunt alternative is \dfrac inside a plain cases. It works for fractions and nothing else — sums, limits and integrals stay cramped — so once a document has more than one awkward branch, switch to dcases and be done. mathtools also gives you dcases*, which sets the condition column in upright text automatically, no \text needed.
Brace on the right: rcases
When several expressions collapse into one conclusion, the brace goes on the right. mathtools' rcases (and its displaystyle sibling drcases) handles it:
Numbering individual branches: numcases
Plain cases lives inside one equation and gets at most one number. If a proof needs to cite branch two on its own, use numcases from the cases package (confusingly named, unrelated to amsmath):
Note the shape: the left-hand side goes in the braces after \begin{numcases}, and the condition column is plain text mode — hence $x \ge 0$ with its own dollars, the opposite of the \text dance in amsmath cases.
Punctuation and style
- Comma between value and condition:
x, & \text{if } x \ge 0. House styles differ on whether the comma is required; being consistent is what matters. - An equation ending a sentence takes a period after the last branch — most journals copy-edit this in if you leave it out.
- Keep conditions parallel: all
\text{if } ...with a final\text{otherwise}, not a mix of "if", "for" and "when".
Nested cases: legal, ugly, avoid
A cases inside a cases compiles fine and reads terribly — the inner brace crowds the outer one and the condition columns stop lining up. Restructure instead: flatten the conditions into compound ones, or split the definition into two displayed equations.
Rule of thumb: if a piecewise definition needs nesting, the notation is doing a job that prose or a second function definition would do better.
Frequently asked questions
How do I write a piecewise function in LaTeX?
Use the cases environment from amsmath: f(x) = \begin{cases} x & \text{if } x \ge 0 \\ -x & \text{otherwise} \end{cases}. The & separates the value from its condition, \\ starts the next branch, and the left brace is drawn for you.
Why do fractions look tiny inside cases?
cases sets its rows in textstyle, so \frac shrinks. Load mathtools and use dcases instead of cases — same syntax, displaystyle rows. The blunt alternative is writing \dfrac in an ordinary cases, but dcases fixes integrals, sums and limits too, not just fractions.
How do I put the brace on the right side instead of the left?
mathtools provides rcases (and drcases for the displaystyle version): \begin{rcases} ... \end{rcases} draws the brace on the right, which is the convention when several expressions collapse into one conclusion.
Can each branch of a piecewise function get its own equation number?
Yes — the numcases environment from the cases package: \begin{numcases}{f(x)=} branch & condition \\ ... \end{numcases}. Each branch gets a number you can \label and reference individually, which plain cases cannot do.
Getting Started
Text Formatting
Mathematical Expressions
Document Structure
Page Layout
Errors & Troubleshooting
