If you are also a LaTeX user and wants to try out the piecewise function, don’t worry. This tutorial has different ways to write and use a piecewise function in LaTeX.

**How to Write and Use the Piecewise Function in LaTeX**

Let’s start with a simple example of using a piecewise function. You are writing an equation, so add the amsmath usepackage and \begin{equation} with the source code.

\usepackage{amsmath}

\begin{document}

\begin{equation}

f(x) =

\left\{

\begin{array}{cc}

-x^2y & \quad x \leq 0 \\

xy^2 & \quad x \geq 0

\end{array}

\right\} = xy

\end{equation}

\end{document}

**Output:**

If you use the \begin{array}{lr} rather than \begin{array}{cc}, you will get a bit different result. For example, we used the \begin{array}{lr} and \begin{array}{cc} one by one and got the following output after compiling:

\usepackage{amsmath}

\begin{document}

\begin{equation}

f(a) =

\left\{

\begin{array}{cc}

5-a & a\leq 0 \\

\frac{25-a}{5} & 0\leq a\leq 25 \\

0 & 25\leq a

\end{array}

\right\} = 25a

\end{equation}

\end{document}

**Output:**

\usepackage{amsmath}

\begin{document}

\begin{equation}

f(a) =

\left\{

\begin{array}{lr}

5-a & a\leq 0 \\

\frac{25-a}{5} & 0\leq a\leq 25 \\

0 & 25\leq a

\end{array}

\right\} = 25a

\end{equation}

\end{document}

**Output:**

As both output shows, the results are slightly different from each other. The \begin{array}{cc} source provides more standard output than \begin{array}{lr}.

**Conclusion**

This was the brief information on the approach that you use to write a piecewise function in LaTeX. LaTeX is a fantastic document processor, but it requires you to learn the correct source codes to get the right result. If you also want to know about these source codes, visit our website. We have written hundreds of LaTeX-related tutorials, so make sure you check them out.