Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?
share|improve this question
add comment

1 Answer 1

up vote 10 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.