Alignment across nested aligned environments

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}

Sample output

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.


This is an improved version of @AndrewSwann's answer take scales properly if one replaces the ddd from the example with something considerably larger and does away with the magical 7mu. I've added an example that shows how to nest it, too.

\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage{calc}
\usepackage{xparse}
\newlength\mytemplena
\newlength\mytemplenb
\DeclareDocumentCommand\myalignalign{sm}
{
  \settowidth{\mytemplena}{$\displaystyle #2$}%
  \setlength\mytemplenb{\widthof{$\displaystyle=$}/2}%
  \hskip-\mytemplena%
  \hskip\IfBooleanTF#1{-\mytemplenb}{+\mytemplenb}%
}
\begin{document}
\begin{align}
  a &= 1\\
  bb &= 1\\
  \myalignalign{eeeeee} &\left.
    \myalignalign*{eeeeee}
    \begin{aligned}
      \myalignalign{dddd} &\left.
        \myalignalign*{dddd}
        \begin{aligned}
          ccc &= 1 \\
          dddd &= 123\\
        \end{aligned} \quad
      \right\} Z1,Z2\\
      eeeeee &= 12345678\\
    \end{aligned} \quad
  \right\} Z1,Z2,Z3
\end{align}
\end{document}

rendering