12

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?

2

3 Answers 3

15

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}
1
  • I was hoping in something that does not require some sort of manual spacing, that I could also use in more complicated examples. Thank you anyway!
    – antonio
    Feb 4, 2013 at 17:16
11

You can enclose one of the entries in the first cases environment in a box whose width is that of the longest of the second cases environment.

\newlength{\temp}
\settowidth{\temp}{$-\sqrt{-x}$}
\begin{align}
f(x) &= 
  \begin{cases}
    \makebox[\temp][l]{$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}

enter image description here

3

Use a combination of hphantom and mathrlap provided by the package mathtools. Put the longest expression in hphantom and wrap mathrlap around the expression that hphantom is placed after.

In your example, the longest expression is -\sqrt{-x}. To place hphantom where x is, use

\mathrlap{x}\hphantom{-\sqrt{-x}}

For example:

\documentclass{article}
\usepackage{amsmath,mathtools}
\begin{document}
\begin{align}
  f(x) &= \begin{cases}
    \mathrlap{x}\hphantom{-\sqrt{-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}
\end{document}
1
  • Please post a full MWE, not code snippets.
    – Mico
    Apr 21, 2015 at 10:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .