# Why does the & reset grouping?

In this answer, I used \tikzmark to mark the extent of a typeset piece of text, so I could draw changebars next to it. But it initially seemed that the changebars "disappear" with math-mode content. After some debugging, I whittled it down to the following MWE:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{amsmath}
\newlength\aLength
\newcommand{\tester}[1]{%
\setlength{\aLength}{50pt}%
\def\aMacro{Hello}%
\typeout{Before: \the\aLength, and \aMacro}%
#1%
\typeout{After: \the\aLength, and \ifdef{\aMacro}{\aMacro}{undefined!!}}
}%

\begin{document}
\begin{align*}
\tester{0}
\tester{ 0 & 1 }
\end{align*}
\end{document}


This yields the following behavior:

Before: 50.0pt, and Hello
After: 50.0pt, and Hello
Before: 50.0pt, and Hello
After: 0.0pt, and undefined!!
Before: 50.0pt, and Hello
After: 50.0pt, and Hello
Before: 50.0pt, and Hello
After: 0.0pt, and undefined!!


I have two questions on this output:

1. Why are there four sets of output? The MWE calls \tester only twice.
2. What exactly is & doing to cause \aLength and \aMacro to revert to their previous values?
-

align builds an alignment similar in essence to a tabular; each cell forms a group and there's not much to do about this except to perform global assignments:

\global\aLength=50pt\relax


Moreover, align reads its contents twice: the first time to measure the columns and the second time to do the actual typesetting.

The conditional it uses to see if it's on the first step is \ifmeasuring@; so

\ifmeasuring@\typeout{...}\fi


will show the message only once. You have to choose whether doing the (global) assignments during both steps or only once. Pay attention to the @ in the name.

-
Thanks, I was afraid it was something like that. Any thoughts on question part #1? –  Ben Lerner Jul 22 '11 at 19:45
Here's the explanation –  egreg Jul 22 '11 at 19:56
Nice, thanks! :) –  Ben Lerner Jul 22 '11 at 20:04