I'm trying to define my own custom environment, and ran into this problem: When I set a macro inside the environment's start code, that macro loses its value at the end of the environment.
To be more precise, with the following LaTeX code
\def\foo{init}
\newenvironment{test}{
entering test environment, foo = \foo\\
\def\foo{bar}
updated to foo = \foo\\
}{
exiting test environment, foo = \foo\\
}
\begin{test}
inside environment: foo = \foo\\
\end{test}
outside environment: foo = \foo\\
\begin{test}
inside environment: foo = \foo\\
\end{test}
outside environment: foo = \foo\\
I get the following output:
entering test environment, foo = init
updated to foo = bar
inside environment: foo = bar
exiting test environment, foo = bar
outside environment: foo = init
entering test environment, foo = init
updated to foo = bar
inside environment: foo = bar
exiting test environment, foo = bar
outside environment: foo = init
As you see, the value of the macro seems to reset right at the end of the environment's end code. This poses two questions:
What is going on here?
Is this some kind of local scope with a local macro that shadows the global macro's value? Or is it some other mechanism that I have not yet understood?How can I assign a value to a macro inside an environment in such a way, that I can actually make use of it afterwards?
\begin{test}
implicitly starts a group and\end{test}
ends it. That means that all assignment that happen in your environment are local to the environment. If you use\gdef\foo{bar}
the assignment is global and will be available outside the environment as well (and indeed globally).\gdef
seems to be exactly what I need. Would you mind fleshing that out in an answer so that I may vote on it?