In mathematical writing it is common to have to distinguish between different possible cases or to define a **piecewise function**, that is, a function whose expression depends on the subset we are evaluating it. For the purpose of writing this kind of expression, LaTeX and some external packages provide different tools. **Our goal is to explore some of these tools and put them into practice**.

## 1. Create piecewise functions using array environment

Of course, the external package we will be using for most of the tools is the amsmath package. This package provides multiple tools that are very useful when writing mathematical content in LaTeX. In particular, with this package, w**e can use the array environment inside math mode to produce arrays of cases**, as in the following example:

% Cases functions in LaTeX \documentclass{article} % Required package \usepackage{amsmath} \begin{document} \begin{equation} Y(i,k) = \left\{ \begin{array}{lr} ||R_{k}-R_{i}||^{2}, & \text{if } i \neq k\\ ||\triangle_{i}||^{2}, & \text{if } i\leq k \end{array} \right\} = yz \end{equation} \end{document}

which produces the following output:

- Basically, the array environment takes as a
**mandatory argument the alignment of the columns**that will appear in the environment (which can be l for left, c for center and r for right) - and uses
**& to separate the different columns**, - and
**\\ to break lines**.

As you can see, the alignment of the two columns used in the previous example is different. This depends on the aspect of the desired output, but in general, there is no unique or standard solution.

## 2. A simplified method using cases environment

Although this array environment is very useful and flexible, is not the easiest way to produce this kind of case situation. As it is usual,** the amsmath package provides an easier and more user-friendly way to typeset piecewise-defined functions**, that can also be used in other situations.

For example, if we also include the **amssymb package just for some symbols**, we can produce the output:

with the simple code:

% Typeset piecewise functions using cases \documentclass{article} % Required package \usepackage{amsmath,amssymb} \begin{document} \begin{equation} \chi_{\mathbb{Q}}(x)= \begin{cases} 1 & \text{if } x \in \mathbb{Q}\\ 0 & \text{if } x \in \mathbb{R}\setminus\mathbb{Q} \end{cases} \end{equation} \end{document}

Observe that with cases we also use & to separate the columns and \\ to separate the rows, but the use is much

more straightforward since we don’t have to specify the columns beforehand.

We reached the end of this tutorial, If you have any remarks or suggestions, please feel free to reach us via email at admin@latex-tutorial.com