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*}
Best Answer
Since you're not really using an array, I suggest another strategy:
The left side is specified by
\X{...}
, the right side by\Y{...}
.