# Array alignment within align environment

I am trying to typeset an array within an align environment, aligning to a specific separator in the array. In the following example, I want to align on the ":" separator in the array.

\documentclass{article}
\usepackage{amsmath, array}

\begin{document}
\begin{align*}
x^2 & = y^2 \\
z^2 &= a^2 \\
\begin{array}{l@{:}c}
a & b\\
c & d
\end{array}
\end{align*}
\end{document}


Should I be using an alignat environment for this? That usually leads to a whole bunch of extra "&"s floating around...

MAJOR EDIT:

Thank you to all the users who replied. I have updated my MWE with each of the solutions provided and entered my comments along with them. Basically each of the solutions solves an aspect of the problem. One solves the alignment issue and one allows me to add columns in the array environment which might not exist in the original align environment. Further comments are very welcome.

\documentclass{article}
\usepackage{amsmath, array}

\makeatletter
\newcommand{\X}[1]{\ifmeasuring@ #1\else\fgnu@X{#1}\fi}
\newcommand{\Y}[1]{\ifmeasuring@ {}:#1\else\fgnu@Y{#1}\fi}
\def\fgnu@X#1{\hbox to \ifcase1\maxcolumn@widths\fi{$\displaystyle#1$\hfil}}
\def\fgnu@Y#1{\hbox to \ifcase2\maxcolumn@widths\fi{${}:{}$\hfil$\displaystyle#1$\hfil}}
\makeatother

\begin{document}
%================================================
% original problem
\begin{align*}
x^2 & = y^2 \\
z^2 &= a^2 \\
\begin{array}{l@{:}c}
a & b\\
c & d
\end{array}
\end{align*}

%================================================
% egreg's solution
%   Cannot extend to more columns in the array than
%   in the original align environment
\begin{align*}
x^2 & = y^2 \\
z^2 &= a^2 \\
\X{aaaa} & \Y{bbbbbggggggggg}\\
\X{cccccccc} & \Y{dd}
\end{align*}

%================================================
% David Carlisle's solution
%   This solution allows me to add more columns to
%   the array, but, the ":"' is not centred to the
%   "="' in the align environment.
\begin{align*}
x^2 & = y^2 \\
z^2 &= a^2 \\
\begin{array}{ll@{:}}
a &a\\
c & c
\end{array}
&
\begin{array}{@{}cc}
b&b\\
d&d
\end{array}\\
\end{align*}

%================================================
% Barbara Beeton's solution
%   Nice alignment solution, however, can I add more
%   columns to the array?
\begin{gather*}
\begin{aligned}
x^2 & = y^2 \\
z^2 &= a^2
\end{aligned}\\
\begin{array}{l@{:}c}
a & b\\
c & d
\end{array}
\end{gather*}

-
Is this the actual content that you want aligned, or is this just a sample of things in the form of an MWE? You can do either everything in an align*, or everything in an array. Do you have a preference, or just get 'er done? What about the spacing around :... is that what you're after (very tight)? Also, your array column specification l.c differs from the alignment within align, which is r.l. Which should it be? –  Werner Jul 13 '12 at 4:08
No, my actual problem is a lot more complicated. I have a lot of equations aligned on the "=" before and after the array and I don't want to have to leave the align environment to typeset the array because that breaks the alignment of the equations. I want to align the align environment to the column of the array that makes my entire set look neatest, else everything in my align environment gets pushed to one side. Spacing is not important for the ":", but I understand mathrel thanks to user Mico. –  fg nu Jul 13 '12 at 4:14
Obviously, the array is expected to have more than 2 columns (about 6) and I want to align it to one of the middle columns. –  fg nu Jul 13 '12 at 4:16
@Werner I am not sure my question is very clear to read, so please feel free to make edits to make it clearer and/or ask more questions about it. –  fg nu Jul 13 '12 at 4:18

Since you're not really using an array, I suggest another strategy:

\documentclass{article}
\usepackage{amsmath, array}
\makeatletter
\newcommand{\X}[1]{\ifmeasuring@ #1\else\fgnu@X{#1}\fi}
\newcommand{\Y}[1]{\ifmeasuring@ {}:#1\else\fgnu@Y{#1}\fi}
\def\fgnu@X#1{\hbox to \ifcase1\maxcolumn@widths\fi{$\displaystyle#1$\hfil}}
\def\fgnu@Y#1{\hbox to \ifcase2\maxcolumn@widths\fi{${}:{}$\hfil$\displaystyle#1$\hfil}}
\makeatother

\begin{document}
\begin{align*}
x^2 & = y^2 \\
z^2 &= a^2 \\
\X{aaaa} & \Y{bbbbbggggggggg}\\
\X{cccccccc} & \Y{dd}
\end{align*}
\end{document}


The left side is specified by \X{...}, the right side by \Y{...}.

-
Thanks. I have added some more information to the problem, so hopefully you can take another look? –  fg nu Jul 14 '12 at 8:33
@FgNu I can't if you don't explain clearly what you want to achieve, possibly with a "real world" example. –  egreg Jul 14 '12 at 20:33

It's not strictly necessary but easiest is to change the input order and input the two halves of the array separately. I made it a 4 column array to be slightly clearer:

\documentclass{article}
\usepackage{amsmath, array}

\begin{document}
\begin{align*}
x^2 & = y^2 \\
z^2 &= a^2 \\
\begin{array}{ll@{:}}
a &a\\
c & c
\end{array}
&
\begin{array}{@{}cc}
b&b\\
d&d
\end{array}\\
\end{align*}
\end{document}

-
i think the array is wanted centered on the page, not offset to one side. read the comments added to the question itself. –  barbara beeton Jul 13 '12 at 12:46
@barbarabeeton Comments say the array should be aligned such that the : aligns with the align alignment point, which is what this does as far as I understand the question. –  David Carlisle Jul 13 '12 at 12:51
okay, we interpret it differently; i understood (reading between the lines) that he really wants the array centered horizontally, but doesn't know how to choose the proper alignment point. (there really isn't any.) so i've posted an alternative answer. –  barbara beeton Jul 13 '12 at 13:04
I have added more information to the question, any more comments will be very welcome. Thanks to both @barbarabeeton and davidcarlisle. –  fg nu Jul 14 '12 at 8:32

since you're using amsmath, you have other options. if i understand correctly what you want, gather* with embedded aligned and array will do it.

\documentclass{article}
\usepackage{amsmath,array}

\begin{document}
\begin{gather*}
\begin{aligned}
x^2 & = y^2 \\
z^2 &= a^2
\end{aligned}\\
\begin{array}{l@{:}c}
a & b\\
c & d
\end{array}
\end{gather*}
\end{document}


with this result:

if you have aligned material both above and below the array, getting both parts to align at the same point may be tricky. (i didn't experiment with that.)

-