# Storing contents of environment within align

Now we are at it here is another one that has been puzzling me.

\documentclass[a4paper]{article}
\usepackage{amsmath,environ}
\def\storer#1{\def\data{#1}}
\makeatletter
\newenvironment{test}{\Collect@Body\storer}{%
\begin{pmatrix}
\data
\end{pmatrix}
}
\makeatother
\begin{document}
This works
\begin{equation*}
\begin{test}
a & b \\ c & d
\end{test}
\end{equation*}
This does not
\begin{align*}
\begin{test}
a & b \\ c & d
\end{test}
\end{align*}
\end{document}


Reason, I'd like to make scalable matrices, i.e. I do not want to add \scalebox{...}{....} around matrices, that is not a good user interface.

Currently I'm using a solution that make use of lrbox and varwidth, it works but it is not pretty.

Joseph Wright mentioned that it may have something to do with the double typesetting witchcraft that align is doing

Ideas?

-
The & inside test confuse the alignment parser. It works if you enclose the test (pseudo)environment in braces. –  egreg Jun 29 '12 at 14:42
again, nasty user interface ;-) –  daleif Jun 29 '12 at 14:47

simplest is to use

\begin{align*}
{\begin{test}
a & b \\ c & d
\end{test}}
\end{align*}


with the extra brace group so it works.

the reason this fail is essentially that the outer alignment doesn't know that the test environment is an alignment so the & are taken as part of the outer alignment and things go wrong.

It's probably possible to avoid the need for the explicit group by adding some

  \ifnum0=}\fi


groups somewhere but the AMS alignments are fragile beasts (that are already collecting the environment body to measure things, so doing it again inside is a bit delicate (and potentially inefficient) You might be better to hook into the \if@mesuring information and extract data that align has already collected rather than re-measuring.

version with ifnum groups added:

\documentclass[a4paper]{article}
\usepackage{amsmath,environ}
\def\storer#1{\def\data{#1}}
\makeatletter
\newenvironment{test}{%
\iffalse{\fi\ifnum 0=}\fi
\Collect@Body\storer}{%
\begin{pmatrix}
\data
\end{pmatrix}%
\ifnum0={}\fi
}

\begin{document}
This works
\begin{equation*} \begin{test}  a & b \\ c & d \end{test}\end{equation*}
This does not
\begin{align*}
\begin{test}
a & b \\ c & d
\end{test}
\end{align*}
\end{document}

-
how does it get around this with the matrix constructions? –  daleif Jun 29 '12 at 15:07
they use funky grouping to hide the inner alignment: see updated code –  David Carlisle Jun 29 '12 at 15:14
Very cool, that will work so much nicer than the lrbox + varwidth` solution I've been using. I'll pass it on to Will, it might be a useful trick to mention in the environ manual. –  daleif Jun 29 '12 at 15:34