How to Write and Use Piecewise Functions in LaTeX – TheLinuxCode (2024)

Piecewise functions, also known as piecewise definitions, are functions that are defined differently on different intervals. In LaTeX, piecewise functions are written using the cases environment. This allows you to define a function with multiple cases based on the input value.

In this comprehensive guide, you‘ll learn how to write and format piecewise functions in LaTeX, including best practices for readability.

Introduction to Piecewise Functions

A piecewise function consists of two or more function definitions that apply over different intervals of the domain. The functions are joined together at certain points, called knots or joints.

For example, consider the function:

$$f(x) = \begin{cases}
x^2 &\text{if } x < 0 \
x+2 &\text{if } 0 \leq x < 4\
2x-1 &\text{if } x \geq 4

This defines f(x) differently depending on whether x is less than 0, between 0 and 4 inclusive, or greater than or equal to 4.

Piecewise functions are useful for representing real-world scenarios where the relationship changes under different conditions. LaTeX provides built-in support for typesetting piecewise functions in a clear, readable format.

Basic Syntax

Here is the basic structure for defining a piecewise function in LaTeX using the cases environment inside math mode:

\begin{equation}f(x) = \begin{cases} \textit{expression} &\text{if } \textit{condition}\\ \textit{expression} &\text{if } \textit{condition}\\ \vdots \end{cases}\end{equation}
  • The cases environment contains the individual function definitions
  • Each case consists of an expression and a condition separated by an ampersand (&)
  • Conditions are formatted as text using \text{if }
  • End each case with two backslashes (\)

For example:

\documentclass{article}\usepackage{amsmath}\begin{document}\begin{equation} f(x) = \begin{cases} x^2 & \text{if } x < 0 \\ x+2 & \text{if } 0 \leq x < 4\\ 2x-1 & \text{if } x \geq 4 \end{cases}\end{equation}\end{document}

This renders as:

f(x) = \begin{cases}
x^2 & \text{if } x < 0 \
x+2 & \text{if } 0 \leq x < 4\
2x-1 & \text{if } x \geq 4

Note that the amsmath package is required to use the cases environment.

Formatting Options

There are a few formatting options you can use within the cases environment:

  • Left-align vs right-align: Use [l] or [r] after begin{cases} to left or right align. Default is left.
  • Numbering: Add [resume] to automatically number each case.

For example:

\begin{equation}f(x) = \begin{cases}[r] x^2 & x < 0 \\ x+2 & 0 \leq x < 4\\ 2x-1 & x \geq 4 \end{cases}\end{equation}

Renders right-aligned cases:

f(x) = \begin{cases}[r]x^2 & x < 0 \
x+2 & 0 \leq x < 4\
2x-1 & x \geq 4

And with numbering:

\begin{equation} f(x) = \begin{cases}[resume] x^2 & x < 0 \\ x+2 & 0 \leq x < 4\\ 2x-1 & x \geq 4 \end{cases}\end{equation}


f(x) = \begin{cases}[resume]\text{(i)} & x^2 & x < 0 \
\text{(ii)} & x+2 & 0 \leq x < 4\
\text{(iii)} & 2x-1 & x \geq 4

array vs cases Environment

You may sometimes see piecewise functions formatted using the array environment instead of cases, like this:

\begin{equation}f(x) = \left\{ \begin{array}{cl} x^2 & \text{if } x < 0 \\ x+2 & \text{if } 0 \leq x < 4 \\ 2x-1 & \text{if } x \geq 4 \end{array} \right.\end{equation} 

The array environment works but does not automatically align the conditions and gives less spacing between the cases. The cases environment is preferred for piecewise functions.

Aligning Multiple Functions

You can align multiple piecewise functions vertically using the align environment:

\begin{align} f(x) &= \begin{cases} x^2 & x<0 \\ x+2 & 0 \leq x < 4 \\ 2x-1 & x \geq 4 \end{cases}\\ g(x) &= \begin{cases} -x & x \leq 0 \\ x/2 & x > 0 \end{cases}\end{align}

This aligns the = signs:

f(x) &= \begin{cases}
x^2 & x<0 \
x+2 & 0 \leq x < 4 \
2x-1 & x \geq 4
g(x) &= \begin{cases}
-x & x \leq 0 \
x/2 & x > 0

Make sure to add the appropriate number of line breaks (\) to space out the functions vertically.

Best Practices

Follow these guidelines for clearly formatted piecewise functions:

  • Use the cases environment for piecewise definitions
  • Separate the function expression and condition with an ampersand &
  • Format the condition statements consistently using \text{if }
  • Left align cases by default but use [r] to right align if needed
  • Number cases only if needed for reference using [resume]
  • Break before and after textual condition statements
  • Align multiple functions vertically with ample whitespace


  • Introduce simple, single-variable cases first
  • Use descriptive variable names
  • Format condition statements grammatically
  • Avoid unnecessary braces or punctuation
  • Reference cases by name or number if needed later

Proper formatting makes your piecewise functions more readable and easier to understand.

Visualizing Piecewise Functions

You can use the TikZ package to create graphs visualizing your piecewise functions.

For example:

\documentclass{article}\usepackage{amsmath}\usepackage{pgfplots} \begin{document}\begin{tikzpicture}\begin{axis}[xlabel=$x$, ylabel=$f(x)$, every axis plot/.append style={very thick}]\addplot [blue,domain=-2:0,samples=100] {x^2}; \addplot [red,domain=0:4,samples=100] {x+2};\addplot [green!70!black,domain=4:8,samples=100] {2*x-1};\end{axis}\end{tikzpicture}\end{document}

This generates the plot:

How to Write and Use Piecewise Functions in LaTeX – TheLinuxCode (1)

The graph makes the behavior of the piecewise function visual and easier to understand.

You can reference these plots within your math environments like normal figures.

Referencing Piecewise Functions

When writing long mathematical documents, you may need to reference equations containing piecewise functions multiple times.

There are two main ways to do this in LaTeX:

  1. Label the equation: Use \label{eq:piecwise} after the equation to label it, then reference it with \ref{eq:piecwise}
  2. Define a new piecewise function command:

For example:

\newcommand{\fpiecewise}{ \begin{cases} x^2 & x< 0 \\ x+2 & 0 \leq x < 4\\ 2x-1 & x \geq 4 \end{cases}}

Then you can reuse \fpiecewise throughout the document.

Common Errors

Some common errors when writing piecewise functions in LaTeX include:

  • Forgetting to import the amsmath package
  • Missing ampersands & between the expression and condition
  • Mismatched parenthesis or braces
  • Improper or missing spacing around \text
  • Alignment issues from missing line breaks \\
  • Plotting piecewise graphs with discontinuous lines

Carefully check your cases environment syntax and formatting to avoid errors. Referring to the examples in this guide can help debug issues.


Piecewise functions are an essential mathematical tool supported by LaTeX cases syntax. By following best practices, you can write readable piecewise definitions easily inserted into equations. Visualize your pieces with TikZ graphs and properly reference them throughout your document. You‘re now ready to utilize piecewise functions professionally in your own LaTeX projects!

You maybe like,

How to Write and Use Piecewise Functions in LaTeX – TheLinuxCode (2024)
Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 5321

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.