[Tex/LaTex] How to align multiple cases environment simultaneously

caseshorizontal alignment

In one big align environment, I want to put some cases environments. I would like the alignment of the cases environments to be the same. For example, if I write:

\begin{align}
f(x) &= 
  \begin{cases}
    x & \text{if } x\geq 0,\\
    -x & \text{if } x <0,
  \end{cases}\\
g(x) &=
  \begin{cases}
    \sqrt{x} & \text{if } x \geq 0,\\
    -\sqrt{-x} & \text{if } x < 0.
  \end{cases}
\end{align}

then the equation looks ugly because the two cases environment are aligned at different points. Is there a way to force the alignment point of the two environment to be the same?

Best Answer

You could also use a \hphantom{} to add the appropriate amount of horizontal space:

enter image description here

Code:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
f(x) &= 
  \begin{cases}
    x\hphantom{-\sqrt{-}} & \text{if } x\geq 0,\\
    -x & \text{if } x <0,
  \end{cases}\\
g(x) &=
  \begin{cases}
    \sqrt{x} & \text{if } x \geq 0,\\
    -\sqrt{-x} & \text{if } x < 0.
  \end{cases}
\end{align}
\end{document}
Related Question