# 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}{\ifmeasuring@ #1\else\fgnu@X{#1}\fi}
\newcommand{\Y}{\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, 2012 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. Jul 13, 2012 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. Jul 13, 2012 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. Jul 13, 2012 at 4:18

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

\documentclass{article}
\usepackage{amsmath, array}
\makeatletter
\newcommand{\X}{\ifmeasuring@ #1\else\fgnu@X{#1}\fi}
\newcommand{\Y}{\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? Jul 14, 2012 at 8:33
• @FgNu I can't if you don't explain clearly what you want to achieve, possibly with a "real world" example. Jul 14, 2012 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. Jul 13, 2012 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. Jul 13, 2012 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. Jul 13, 2012 at 13:04
• I have added more information to the question, any more comments will be very welcome. Thanks to both @barbarabeeton and davidcarlisle. Jul 14, 2012 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.)
{"ft_lang_label":"__label__en","ft_lang_prob":0.7272121,"math_prob":0.8817493,"size":3081,"snap":"2022-27-2022-33","text_gpt3_token_len":922,"char_repetition_ratio":0.20052,"word_repetition_ratio":0.11363637,"special_character_ratio":0.34599155,"punctuation_ratio":0.0952381,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9733231,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,4,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-25T05:11:57Z\",\"WARC-Record-ID\":\"<urn:uuid:1aeefd9d-37ba-4b97-a90b-ac38aa5fe6b2>\",\"Content-Length\":\"259882\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f532b35e-3e26-4317-a39a-48e9d746a8a6>\",\"WARC-Concurrent-To\":\"<urn:uuid:932c70eb-d2ba-4e91-9010-480d29e37d34>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/63127/array-alignment-within-align-environment\",\"WARC-Payload-Digest\":\"sha1:QF7YRGB3X27GNJBOSCXCQIT4XBA7ATYK\",\"WARC-Block-Digest\":\"sha1:UOXDIIPHCRWGXZU3DGWBMANCYW5BAKAB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103034170.1_warc_CC-MAIN-20220625034751-20220625064751-00391.warc.gz\"}"}