When writing a math definition of a function, for example, the function may have different results depending on the value of the inputs. These are called cases and are grouped together with a large left curly brace. If you’re trying to typeset this in LaTeX, the cases environment makes this nice and easy.

It’s easiest to describe this with an example. The following code prints the definition a function for calculating the nth number in the Fibonacci sequence:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{equation*}
    f(n) = \begin{cases}
               0               & n = 0\\
               1               & n = 1\\
               f(n-1) + f(n-2) & \text{otherwise}
           \end{cases}
\end{equation*}

\end{document}

Which will be shown as:

fibonacci sequence

Basically, the cases environment is like a mini two-column table; the first column in this case being the function value and the second column defining the rules for when this value applies. Like in a table, the two columns are separated by a & symbol. The end of a case is denoted by the end-of-line double backslash \\. You can define as many cases as you like.

Note here that the case definition is inside an equation block. This is a form of math environment. cases will only work in a math environment, but you can use any math environment you like. Also note that you need to use the amsmath package, as highlighted in the example above.




  1. […] A aula seguiu o desenvolvimento de algoritmos para gerar os números da sequência de Fibonacci, partindo de um algoritmo ineficiente e apresentando em seguida algoritmos mais eficientes, motivando o estudo da disciplina. A sequência de Fibonacci é definida por(latex): […]