Piecewise Function in LaTeX
Write a piecewise function with amsmath's cases environment: f(x) = \begin{cases} x^2 & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}. Each piece is one line ending in \\, & separates the value from its condition, and the left brace is drawn for you.
Piecewise syntax
\begin{cases} a & \text{if } x > 0 \\ b & \text{otherwise} \end{cases}Brace on the left, value then condition on each line.
- Text mode, no math preview
&Separates the value column from the condition column.
\text{if } x > 0Upright words in the condition (amsmath).
\begin{dcases} \frac{1}{x} & x \ne 0 \\ 0 & x = 0 \end{dcases}Display-size fractions inside the cases — mathtools.
\begin{rcases} a & x > 0 \\ b & x \le 0 \end{rcases}Brace on the right instead — mathtools.
\left\{ \begin{array}{ll} a & x > 0 \\ b & x \le 0 \end{array} \right.The manual way with array; lets you choose column alignment.
- Text mode, no math preview
\begin{numcases}{f(x)=} … \end{numcases}Numbers each line separately — cases package; conditions are in text mode.
Try it live
Change the formula and the preview redraws as you type. Copy the LaTeX, download a PNG or SVG, copy it for Word, or use Edit here on any example below to load it.
Piecewise functions (cases) examples
Each result below is drawn from the LaTeX under it. Copy the code, edit it on this page, or open it in the full editor.
Absolute value
LaTeX|x| = \begin{cases} x & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}Sign function (three pieces)
LaTeX\operatorname{sgn}(x) = \begin{cases} -1 & \text{if } x < 0 \\ 0 & \text{if } x = 0 \\ 1 & \text{if } x > 0 \end{cases}Heaviside step function
LaTeXH(x) = \begin{cases} 0, & x < 0 \\ 1, & x \ge 0 \end{cases}Full-size fractions with dcases
LaTeXg(x) = \begin{dcases} \frac{\sin x}{x} & x \ne 0 \\ 1 & x = 0 \end{dcases}Plain
caseswould shrink the fraction to inline size.Recursive definition
LaTeXF_n = \begin{cases} 0 & n = 0 \\ 1 & n = 1 \\ F_{n-1} + F_{n-2} & n \ge 2 \end{cases}Brace on the right
LaTeX\begin{rcases} x^2 & x \ge 0 \\ -x^2 & x < 0 \end{rcases} = x\,|x|
How to write a piecewise function
Load \usepackage{amsmath} and put the function in math mode, for example \[ f(x) = \begin{cases} … \end{cases} \]. Write one piece per line: the value, then &, then the condition, and end every line except the last with \\.
Wrap words in the condition in \text{} and include their spaces: \text{if } x > 0. Without it, "if" is set as two italic variables. A comma after the value (0, & x < 0) is a common alternative to "if".
dcases, rcases and numbered cases
cases sets its content in text style, so fractions and sums shrink. The mathtools package adds dcases (display style), rcases (brace on the right) and dcases* (condition column in text mode).
To give each line its own equation number, the cases package offers numcases: \begin{numcases}{f(x)=} x & for $x \ge 0$ \\ -x & for $x < 0$ \end{numcases}. Its condition column is text, so math there needs $ … $.
Common mistakes
Without
&the conditions do not line up, and without\text"if" is printed as italic variables with no spaces.Instead off(x) = \begin{cases} x^2 if x \ge 0 \\ -x if x < 0 \end{cases}Writef(x) = \begin{cases} x^2 & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}casesis math-only; in running text you get "Missing $ inserted".Instead of\begin{cases} … \end{cases} (outside math)Write\[ f(x) = \begin{cases} … \end{cases} \]A piecewise function has no closing brace;
\right.is the invisible partner that\left\{requires.Instead of\left\{ \begin{array}{ll} … \end{array} \right\}Write\left\{ \begin{array}{ll} … \end{array} \right.A
\\after the last line adds an empty row and extra space under the brace.Instead of… \\ 0 & x = 0 \\ \end{cases}Write… \\ 0 & x = 0 \end{cases}
In Overleaf, MathJax and Word
Overleaf and pdfLaTeX
casesneeds amsmath;dcasesandrcasesneed\usepackage{mathtools}(which loads amsmath);numcasesneeds the cases package.MathJax and KaTeX
MathJax supports
cases;dcasesandrcasesneed its mathtools extension (MathJax 3.2 or later), which the editor on this page loads. KaTeX supportscases,dcases,rcasesanddrcases. In Markdown write it between$$ … $$.Microsoft Word
Word has its own brace-and-cases structures in the equation tools. The quickest path from LaTeX is to type the function in the editor on this page and use Copy for Word, which pastes it as an editable Word equation.
Piecewise functions (cases) in LaTeX: questions
- How do I write a piecewise function in LaTeX?
- Use
f(x) = \begin{cases} value & \text{condition} \\ value & \text{condition} \end{cases}inside math mode with amsmath loaded. - How do I add "if" or "otherwise" to a piecewise function?
- Put the words in
\text{}:0 & \text{otherwise}. Leave a space inside the braces when a formula follows:\text{if } x > 0. - Why are the fractions in my cases so small?
casesuses text style. Switch todcasesfrom mathtools, or write\dfracfor individual fractions.- How do I put the brace on the right side?
- Use
rcasesfrom mathtools, or\left. \begin{array}{ll} … \end{array} \right\}.
Related LaTeX reference
- How to Write a Multiline Equation in LaTeXalign and align* for aligned lines, aligned inside $$ … $$, split, gather and multline for long formulas and systems of equations.
- Absolute Value in LaTeX|x|, \lvert x \rvert, bars that stretch with \left| \right|, double bars for norms, and an \abs macro with mathtools.
- Math Mode in LaTeXInline math with $…$ or \(…\), displayed math with \[…\], numbered equation environments, references and \text inside formulas.
- LaTeX equation editorSymbol panel, live preview, and PNG, SVG, MathML or Word export.
- All LaTeX reference pagesSymbols, math structures and text formatting in one list.
Have the formula as a picture?
Paste a screenshot or photo of the equation and get its LaTeX, ready to copy or open in the editor.
Convert an image to LaTeX