HOW TO WRITE PIECEWISE FUNCTIONS IN LATEX
Piecewise functions in LaTeX with cases
Examples checked with MathTexLab’s base and AMS math renderer · Display style
Use cases to write a piecewise function in LaTeX. Each row
contains a formula, an & alignment marker and a condition.
Separate branches with \\; the environment supplies the left
brace.
Build the branches and conditions
The absolute-value example uses one branch when x is nonnegative and another when x is negative. Each input belongs to exactly one of these conditions, including zero. Change the formulas and conditions together when adapting it.
Put short words such as “if” inside \text{if }, and keep
inequalities outside that text command. The trailing space separates the
word from the variable. In a full LaTeX document, load
amsmath and place the expression in math mode.
|x|=\begin{cases}x & \text{if }x\ge 0\\-x & \text{if }x<0\end{cases}Open example in editor For three branches, add a row with the same formula-and-condition pattern. Put one ampersand before each condition so the conditions line up. A comma before the ampersand is optional punctuation, not a column separator.
f(x)=\begin{cases}x^2,&x<0\\0,&x=0\\\sqrt{x},&x>0\end{cases}Open example in editor Fix missing braces and broken rows
Do not add another opening brace around the cases environment. If the second branch appears on the first row, check that the source contains two backslashes between branches. A line break in the source alone does not create a mathematical row.
Match \begin{cases} with \end{cases}. The editor
displays your conditions but does not detect missing intervals, overlapping
branches or an incorrect definition.
Edit and reuse the function
Open the example in the Equation Editor, change one branch and wait for the preview before exporting. Undo restores your previous source after loading a guide example. Keep a copy of the source if you will need to edit the formula again.
For several equalities rather than conditional branches, use the aligned-equations guide. For labels inside a condition, see text inside equations.
Prefer separate expression and condition fields? Use the LaTeX Piecewise Function Generator, then open its output in the Equation Editor for image exports.
Syntax reference
Continue with LaTeX Equation Editor.