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}
    { a if  x > 0 b otherwise

    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 > 0
    if  x > 0

    Upright words in the condition (amsmath).

  • \begin{dcases} \frac{1}{x} & x \ne 0 \\ 0 & x = 0 \end{dcases}
    { 1 x x ≠ 0 0 x = 0

    Display-size fractions inside the cases — mathtools.

  • \begin{rcases} a & x > 0 \\ b & x \le 0 \end{rcases}
    a x > 0 b x ≤ 0 }

    Brace on the right instead — mathtools.

  • \left\{ \begin{array}{ll} a & x > 0 \\ b & x \le 0 \end{array} \right.
    { a x > 0 b x ≤ 0

    The manual way with array; lets you choose column alignment.

  • \begin{numcases}{f(x)=} … \end{numcases}
    Text mode, no math preview

    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.

Templates

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

    | x | = { x if  x ≥ 0 − x if  x < 0
    LaTeX
    |x| = \begin{cases} x & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}
  • Sign function (three pieces)

    sgn ( x ) = { − 1 if  x < 0 0 if  x = 0 1 if  x > 0
    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

    H ( x ) = { 0 , x < 0 1 , x ≥ 0
    LaTeX
    H(x) = \begin{cases} 0, & x < 0 \\ 1, & x \ge 0 \end{cases}
  • Full-size fractions with dcases

    g ( x ) = { sin ⁡ x x x ≠ 0 1 x = 0
    LaTeX
    g(x) = \begin{dcases} \frac{\sin x}{x} & x \ne 0 \\ 1 & x = 0 \end{dcases}

    Plain cases would shrink the fraction to inline size.

  • Recursive definition

    F n = { 0 n = 0 1 n = 1 F n − 1 + F n − 2 n ≥ 2
    LaTeX
    F_n = \begin{cases} 0 & n = 0 \\ 1 & n = 1 \\ F_{n-1} + F_{n-2} & n \ge 2 \end{cases}
  • Brace on the right

    x 2 x ≥ 0 − x 2 x < 0 } = x | x |
    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 of
    f(x) = \begin{cases} x^2 if x \ge 0 \\ -x if x < 0 \end{cases}
    Write
    f(x) = \begin{cases} x^2 & \text{if } x \ge 0 \\ -x & \text{if } x < 0 \end{cases}
  • cases is 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

    cases needs amsmath; dcases and rcases need \usepackage{mathtools} (which loads amsmath); numcases needs the cases package.

  • MathJax and KaTeX

    MathJax supports cases; dcases and rcases need its mathtools extension (MathJax 3.2 or later), which the editor on this page loads. KaTeX supports cases, dcases, rcases and drcases. 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?
cases uses text style. Switch to dcases from mathtools, or write \dfrac for individual fractions.
How do I put the brace on the right side?
Use rcases from mathtools, or \left. \begin{array}{ll} … \end{array} \right\}.

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