# Alignment across nested aligned environments

I have a list of equations with annotations like the one below:

\documentclass{scrartcl}
\usepackage{amsmath}
\begin{document}
\begin{align}
aaaa &= 1  &&\text{for $X$} \\
bbbb &= 1  &&\text{for $Y$} \\
c    &= 1  &&\text{for $Z$} \\
d    &= 12 &&\text{for $Z$}
\end{align}
\end{document}


Since the last two lines have the same annotation, I'd like to add a brace there and put the annotation next to the brace, like so:

\documentclass{scrartcl}
\usepackage{amsmath}
\begin{document}
\begin{align}
\left. \begin{aligned}
c &= 1 \\
d &= 12 \\
\end{aligned} \right\} &&\text{for $Z$}
\end{align}
\end{document}


Plugging that into the original code gives me something along the lines of

\documentclass{scrartcl}
\usepackage{amsmath}
\begin{document}
\begin{align}
aaaa &= 1  &&\text{for $X$} \\
bbbb &= 1  &&\text{for $Y$} \\
\left. \begin{aligned}
c &= 1 \\
d &= 12 \\
\end{aligned} \right\}& &&\text{for $Z$}
\end{align}
\end{document}


What doesn't work is the alignment of the equals signs: I can only align the ones inside the aligned environment with eachother, not the ones outside.

I'd also like the align environment to treat the content as three lines and number it as such.

This is a follow-up question to Q: Align inside align in which a solution to the problem without line numbering was given; a similar problem was discussed in Q: Aligning across 'aligned' equation blocks -- the solution used an array environment, however, and thus also does not allow for line numbering afaict.

-
this question might be helpful: how-can-i-align-two-split-environments-at-the-equal-signs – barbara beeton Aug 24 '12 at 12:40

The solution to the question pointed out by Barbara Beeton is close to the right idea. In fact, you could use that solution by putting each object you wish to have numbered in its own aligned. However, that is quite a bit of work and the following variation seems to function well enough:

\documentclass{scrartcl}
\usepackage{amsmath}

\newlength{\myleftlen}
\newcommand{\setmyleftlen}[1]{\settowidth{\myleftlen}{$$\displaystyle #1$$}}
\newcommand{\backup}{\hskip-\myleftlen\mkern-7mu}

\begin{document}

\setmyleftlen{ddd}
\begin{align}
aaaa &= 1  &&\text{for $X$} \\
bbbb &= 1  &&\text{for $Y$} \\
&\left.\backup\begin{aligned}
c &= 1 \\
ddd &= 12 \\
\end{aligned} \right\} &&\text{for $Z$}
\end{align}

\end{document}


You will need to call \setmyleftlen with the longest lefthand side in your aligned environment before the align starts. What the code does is move the aligned block left this amount plus an extra 7mu. The amount 7mu was a guess, but seems to fit with some other values used by the AMS math commands. Being expressed in mu (math units) it will scale well to other point sizes.

-
Thanks a lot -- that appears to solve my problem. I found that 8mu is a better choice for the length in \backup although I don't know why. – anonymous Aug 24 '12 at 19:41

If I replace ddd in the example with dddddddddddddddddddddddddddddddd, everything is shifted to the left quite a bit. The following modification of @AndrewSwann's answer takes care of that.

Update: I've made this a bit nicer to use, so that it can be nested.

\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage{xparse}
\newlength{\mytemplen}
\DeclareDocumentCommand\mybackup{sm}
{
\settowidth{\mytemplen}{$$\displaystyle #2$$}
\hskip-\mytemplen%
\mkern\IfBooleanTF#1{-8mu}{+8mu}
% FIXME: 8mu appears to be okay at font size 11, for larger font
% sizes, 7 appears to work
}
\begin{document}
\begin{align}
a &= 1\\
bb &= 1\\
\mybackup{eeeeee} &\left.
\mybackup*{eeeeee}
\begin{aligned}
\mybackup{dddd} &\left.
\mybackup*{dddd}
\begin{aligned}
ccc &= 1 \\
dddd &= 123\\