[Tex/LaTex] Align inside align

alignamsmathbracesnesting

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. I know that I can do that like this:

\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}

But how to join the two? The last two lines will only be aligned among themselves, not with the first two. Is there a way out of this?

Thanks in advance.

Best Answer

Here is a naive attempt at positioning braces in align:

enter image description here

\documentclass{scrartcl}% http://ctan.org/pkg/koma-script
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{align*}
  aaaa &= 1  &&\text{for $X$} \\
  bbbb &= 1  &&\text{for $Y$} \\
  c    &= 1  && \\
  d    &= 12 &&\llap{\smash{\raisebox{\dimexpr.5\normalbaselineskip+.5\jot}{$\left.\begin{array}{c}\null\\[\jot]\null\end{array}\right\}\quad$}}}
      \text{\smash{\raisebox{\dimexpr.5\normalbaselineskip+.5\jot}{for $Z$}}}
\end{align*}
\end{document}

The combination of \smash (removing any vertical height), \raisebox (for vertical movement) and \llap (removing horizontal width using a left overlap) allows for manipulating the position of the brace.

Of course, the (horizontal) location of the brace can be adjusted.