# Why is \global\let not always global?

As an LaTeX environment opens a local group, it is normally necessary to use a global definition if changes in the environment should be available after the environment:

\documentclass{minimal}
\usepackage{amsmath}
\begin{document}
\begin{multline}
\gdef\mylabel{text}
\end{multline}
\show\mylabel
\end{document}


This results in:

> \mylabel=macro:
->text.


Unfortunately, this does not work with a \global\let. Locally, the definition is correct, as in the following example:

\documentclass{minimal}
\usepackage{amsmath}
\begin{document}
\begin{multline}
\global\let\mylabel\label
\show\mylabel
\end{multline}
\end{document}


This results in:

> \mylabel=macro:
#1->\begingroup \measuring@false \label@in@display {#1}\endgroup .


There is no global effect:

\documentclass{minimal}
\usepackage{amsmath}
\begin{document}
\begin{multline}
\global\let\mylabel\label
\end{multline}
\show\mylabel
\end{document}


This results in:

> \mylabel=\long macro:
#1->.


Why is the \global\let not global here? Is there some (general) work-around to get a \global\let?

Solution (thanks to David Carlisle):

David Carlisle is correct. As amsmath calls the environment twice and the \label command is deleted after the first call, we end up with a deleted command. The solution is to avoid resetting the command in the second call:

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{etoolbox}
\begin{document}
\begin{multline}
\ifundef{\mylabel}{%
\global\let\mylabel\label
}{}%
\end{multline}
\show\mylabel
\end{document}

• I'm curious to know why you want to save the meaning of \label in this way. – egreg Jun 8 '12 at 16:30
• @egreg, does my comment to David Carlisle's answer also answer your question? – Patrick Häcker Jun 8 '12 at 16:35
• Not really, the ams redefinition of \label is a very specific redefinition to work in ams alignments. So if you are in an ams alignment \label will be defined and you don't need \mylabel and if you are not in an ams alignment, you don't want that defintion of \label. – David Carlisle Jun 8 '12 at 16:37
• @DavidCarlisle, contradictory as it may sound, but I do this because multline's \label command is very specific. I need to extend it and with the help of the \global\let, I can create the extended version once. Afterwards I can just renew the environment and do a \let\label\mylabel. – Patrick Häcker Jun 8 '12 at 17:05
• \global\let is still a long winded way of getting the ams definition, which is \def\label#1{\begingroup\measuring@false\label@in@display{#1}\endgroup} you don't need to go into the environment and globally save that definition. – David Carlisle Jun 8 '12 at 17:48

AMS alignments are executed twice internally, a first pass to measure the fields and a second pass to typeset. \label is given a null definition the second time to avoid duplicating writes to the aux file which would generate errors.

So you do a global let of the real definition but you don't see it as you globally let to the null definition on the second pass.

You can see this by putting \show\mylabel inside your multline and see it reporting two values. You can use the \ifmeasuring@ switch to test which pass you are on. and just do the let on one of them.

\documentclass{minimal}
\usepackage{amsmath}

\begin{document}

\makeatletter
\begin{multline}
\ifmeasuring@
\global\let\mylabel\label
\show\mylabel
\fi
\end{multline}
\show\mylabel
\end{document}


Produces

> \mylabel=macro:
#1->\begingroup \measuring@false \label@in@display {#1}\endgroup .
l.14     \show\mylabel

• Moreover, in the first pass \label is redefined in a way that's pretty useless outside an alignment environment. – egreg Jun 8 '12 at 16:21
• @egreg, true but not as useless as its definition on the second pass:-) – David Carlisle Jun 8 '12 at 16:25
• @egreg, you are completely correct, the example above does not make sense, but I wanted to keep it short. In a longer example, the \label command would again be used in a displayed math environment. – Patrick Häcker Jun 8 '12 at 16:33