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*}
align*
, or everything in anarray
. Do you have a preference, or just get 'er done? What about the spacing around:
... is that what you're after (very tight)? Also, yourarray
column specificationl.c
differs from the alignment withinalign
, which isr.l
. Which should it be? – Werner Jul 13 '12 at 4:08align
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 understandmathrel
thanks to user Mico. – tchakravarty Jul 13 '12 at 4:14array
is expected to have more than 2 columns (about 6) and I want to align it to one of the middle columns. – tchakravarty Jul 13 '12 at 4:16