# How to align piecewise functions?

I have two piecewise functions defined like this:

\begin{align*}
\left\{
\begin{array}{ll}
i = x^2 + y^2 \\
j = a^2 + b^2 \\
\end{array}
\right.
\\
\left\{
\begin{array}{ll}
i = 1 \\
j = 2 \\
\end{array}
\right.
\end{align*}


The problem is that they're not aligned. How do I align them?

-
Place an & before the \left\{ of each. –  qubyte Dec 10 '11 at 15:37
It would be better to provide a complete MWE, from \begin{document} to \end{document} with all needed packages. This way it's easier to test your code and provide a solution. –  Ignasi Dec 10 '11 at 15:47

The quick fix is a simple addition of two ampersands before opening the large curly brackets. In the amsmath align environment, along with the starred version, the ampersand tells LaTeX what elements should be aligned.

\begin{align*}
&\left\{
\begin{array}{ll}
i = x^2 + y^2 \\
j = a^2 + b^2 \\
\end{array}
\right.
\\
&\left\{
\begin{array}{ll}
i = 1 \\
j = 2 \\
\end{array}
\right.
\end{align*}


Perhaps the following code using split is a little better to align the = signs, although it's a matter of taste. The ampersand once again allows you to align the brackets on one level, and the equality signs on the level down. This is done inside the starred equation environment to allow split to be used, which is provided by the amsmath package.

\begin{equation*}
\begin{split}
&\left\{
\begin{split}
i &= x^2 + y^2 \\
j &= a^2 + b^2 \\
\end{split}
\right.
\\
&\left\{
\begin{split}
i &= 1 \\
j &= 2 \\
\end{split}
\right.
\end{split}
\end{equation*}


If the reduced space between the bracket and the equations bothers you in this version, you can insert horizontal space, for example with \, or with \hspace{2pt} just after the \left\{ of each group. Using \; spacing in this solution gives the final version of this code:

\begin{equation*}
\begin{split}
&\left\{\;
\begin{split}
i &= x^2 + y^2 \\
j &= a^2 + b^2 \\
\end{split}
\right.
\\
&\left\{\;
\begin{split}
i &= 1 \\
j &= 2 \\
\end{split}
\right.
\end{split}
\end{equation*}


-
Would you also explain why? Perhaps suggesting cases instead of split. –  egreg Dec 10 '11 at 15:45
@egreg: Good point. I added some more detail to explain what the ampersands are for. –  qubyte Dec 10 '11 at 15:51
@egreg: Cases has been suggested at the time of this comment in Gonzalo's answer. –  qubyte Dec 10 '11 at 15:58
@egreg: In addition, cases behave badly when you try to align them on the equalities. –  qubyte Dec 10 '11 at 16:19

To simplify, in this case, you can use two cases environments inside a align* using & to align both structures:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
&\begin{cases}
\rlap{$i$}\phantom{j} = x^2 + y^2 \\
j = a^2 + b^2
\end{cases}\\
&\begin{cases}
\rlap{$i$}\phantom{j} = 1 \\
j = 2
\end{cases}
\end{align*}
\end{document}


-
Cases behave badly when you try to align them on the = symbol. –  qubyte Dec 10 '11 at 16:20
@MarkS.Everitt: I know; that's why I said in my answer in this case. In this concrete example there's no need to use & for the equal symbol. –  Gonzalo Medina Dec 10 '11 at 16:27
To be fair, in this case is a little ambiguous. –  qubyte Dec 10 '11 at 16:29
@MarkS.Everitt: in fact; I've just noticed a little misalignment issue at the = symbols; I've updated my answer to correct this. –  Gonzalo Medina Dec 10 '11 at 16:34

Here is one way of aligning these two components:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{align*}
\left\{
\begin{array}{r@{\;}l}
i &= x^2 + y^2 \\
j &= a^2 + b^2
\end{array}
\right.
\\
\left\{
\begin{array}{r@{\;}l}
i &= \rlap{1}\phantom{x^2 + y^2} \\
j &= \rlap{2}\phantom{a^2 + b^2}
\end{array}
\right.
\end{align*}
\end{document}


You didn't use any column separation & in your array. I've inserted that at the alignment point (before =). Otherwise you would have noticed the difference in the unaligned equality signs due to different widths of i and j. I also changed your ll column specification into r@{\;}l to right-align the left-hand side, and left-align the right-hand side, plus add the appropriate spacing \; between the relational operator.

The use of the \rlap (short for right overlap) and \phantom (leaves space without typesetting) combination is aimed at making the lower array have the same width as the upper one, thereby establishing the appropriate horizontal spacing.

-
Quite complicated a solution, isn't it? A & before \left\{ is all that's needed. –  egreg Dec 10 '11 at 15:44
@egreg: Nope. With only that, the equal signs won't line up. Also, the left braces spacing is slightly modified in split. –  Werner Dec 10 '11 at 15:56
The & would align the braces so the \phantom commands become unnecessary, taking also into account that the two left hand side terms are the same in both arrays. Whether to align these equals signs is just a matter of personal taste. –  egreg Dec 10 '11 at 16:24