Skip to content
All LaTeX symbols

Piecewise functions and cases in LaTeX

Delimiters

{

LaTeX command

\begin{cases} a & x > 0 \\ b & x \le 0 \end{cases}

a brace on the left, one line per case

Write the environment in math mode, usually display math, after the function name and equals sign.

The cases environment draws the large left brace of a piecewise function. Each line has a value and a condition separated by &, and \\ starts the next case. Put words such as "if" and "otherwise" in \text so they are not set as italic variables.

All ways to write it

  • \begin{cases} a & x > 0 \\ b & x \le 0 \end{cases}a brace on the left, one line per casePackage: amsmath
  • \text{if } x > 0words in a condition, in upright textNo package needed
  • \text{otherwise}the catch-all last caseNo package needed
  • \begin{dcases} ... \end{dcases}the same, with fractions at full display sizePackage: mathtools
  • \begin{rcases} ... \end{rcases}the brace on the rightPackage: mathtools

Commands marked with a package need that \usepackage line in a LaTeX document.

Example

|x| = \begin{cases}
  x & \text{if } x \ge 0 \\
  -x & \text{if } x < 0
\end{cases}

The absolute value written as a piecewise function with two cases.

Questions

How do I write a piecewise function in LaTeX?

Use amsmath's cases environment: f(x) = \begin{cases} value & condition \\ value & condition \end{cases}. The & lines the conditions up in a column.

How do I write "if" and "otherwise" in a piecewise function?

Wrap them in \text, for example x^2 & \text{if } x > 0. Leave a space inside the braces after "if", because spaces typed in math mode are ignored.

Why are the fractions small inside cases?

cases sets its entries in text style. Use dcases from mathtools for full-size fractions, or \dfrac for a single entry.

How do I put the brace on the right?

Use rcases from mathtools, which draws the brace on the right of the lines.

Do cases work in KaTeX and Confluence?

Yes. KaTeX renders cases, dcases and rcases. In Confluence use a block math macro so each case gets its own line.

Using piecewise function in Confluence

Confluence Cloud has no built-in LaTeX. With LaTeX Math for Confluence you add a math macro to any page, type \begin{cases} a & x > 0 \\ b & x \le 0 \end{cases} and it renders inline or as a block, with a live preview while you edit. Every command on this page renders there without extra packages.

Related symbols

Also in Delimiters

See all LaTeX symbols