LatexNotes

Piecewise functions in LaTeX

A piecewise function gives a different formula on each part of its domain; in LaTeX the amsmath cases environment draws the left brace, with & between each value and its condition and \\ between the lines.

Main page: Systems of equations in LaTeX →

LaTeX command

f(x) = \begin{cases} x & x \geq 0 \\ -x & x < 0 \end{cases}

Not the symbol you meant? Draw it to find the command →

Typing up handwritten notes? Convert a whole page of handwriting to LaTeX →

Each line of cases has two columns: the value, then the condition, separated by &. Both columns are left-aligned, so the conditions line up neatly however long the values are. End every line but the last with \\. The brace grows to fit however many lines there are.

Conditions usually mix words and maths. Put the words in \text{...} so they stay upright and keep their spaces: \text{if } x > 0, and \text{otherwise} for the last case. A comma after each value, as in x, & , is a common style; follow your course or textbook.

cases sets fractions at the smaller inline size. If a case holds a fraction that should be full size, use dcases from the mathtools package instead, which is otherwise the same. The same package's rcases puts the brace on the right, for a list of cases that lead to one result.

Full-size fractions: dcases (mathtools)

Load mathtools, which also loads amsmath, and write dcases where you would write cases. Its starred form, dcases*, sets the condition column as ordinary text, so the words need no \text.

\usepackage{mathtools}
\begin{dcases} \frac{1}{x} & x \neq 0 \\ 0 & x = 0 \end{dcases}

Other forms

\text{if } x > 0words in a condition
\text{otherwise}the last case
\begin{dcases} ... \end{dcases}full-size fractions inside, mathtools
\begin{rcases} ... \end{rcases}brace on the right, mathtools

Examples

$f(x) = \begin{cases} x & x \geq 0 \\ -x & x < 0 \end{cases}$

the absolute value as a piecewise function

$\operatorname{sgn}(x) = \begin{cases} 1 & \text{if } x > 0 \\ 0 & \text{if } x = 0 \\ -1 & \text{if } x < 0 \end{cases}$

the sign function, with words in the conditions

$f(n) = \begin{cases} n/2 & n \text{ even} \\ 3n + 1 & \text{otherwise} \end{cases}$

otherwise for the last case

$g(x) = \begin{dcases} \frac{\sin x}{x} & x \neq 0 \\ 1 & x = 0 \end{dcases}$

a full-size fraction with dcases

Tips and common mistakes

Working from handwritten notes?

LatexNotes turns a photo or PDF of handwritten mathematics into LaTeX, graphs and all. It is built with real pdflatex before you see it, and the .tex file is yours to edit or take to Overleaf. Free to try, no sign-up.

See how handwriting to LaTeX works, step by step, or go from GoodNotes to LaTeX if you write on an iPad (Notability works the same way).

Convert your maths to LaTeX →

FAQ

How do I write a piecewise function in LaTeX?

Use the cases environment: f(x) = \begin{cases} x & \\ -x & x < 0 \end{cases}. Each line is value & condition, ended with \\.

How do I write 'if' and 'otherwise' in the conditions?

Put them in \text, for example \text{if } x > 0 and \text{otherwise}, so they are upright and keep their spaces.

Why are the fractions in my cases small?

cases sets them at the inline size. Use dcases from the mathtools package for full-size fractions.

Which package does cases need?

amsmath. mathtools loads amsmath and adds dcases and rcases.

See also