The cases environment—and its alignment

The cases environment, made a bit prettier.    #latex #cases #alignment

Consider the following LaTeX code:

\begin{equation}
  \begin{cases}
    \theta \textrm{\quad if \ } \theta \in [0, \pi] \\
    \theta + \theta^2 \textrm{\quad if \ } \theta \in [\pi, 2\pi]
  \end{cases}
\end{equation}

This yields the following:

$$ \begin{equation*} \begin{cases} \theta \textrm{\quad if \ } \theta \in [0, \pi] \\ \theta + \theta^2 \textrm{\quad if \ } \theta \in [\pi, 2\pi] \end{cases} \end{equation*} $$

If this leaves you wondering that it would be nice to have the if’s aligned, I have a simple solution for you: put the shortest preceding text—in the above example, \theta—in the mathrlap command, followed by the longer text (\theta + \theta^2) inside the \hphantom command. The order is important!

The above code would become:

\begin{equation*}
  \begin{cases}
    \mathrlap{\theta}\hphantom{\theta + \theta^2} \textrm{\quad if \ } \theta \in [0, \pi] \\
    \theta + \theta^2 \textrm{\quad if \ } \theta \in [\pi, 2\pi]
  \end{cases}
\end{equation*}

And here is the result:

$$ \begin{equation*} \begin{cases} \mathrlap{\theta}\hphantom{\theta + \theta^2} \textrm{\quad if \ } \theta \in [0, \pi] \\ \theta + \theta^2 \textrm{\quad if \ } \theta \in [\pi, 2\pi] \end{cases} \end{equation*} $$

Which looks so much better! :-)

July 1, 2023. Got feedback? See the contact page.