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}
\label
in this way.\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
.\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
.\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.