Consider this example:
\documentclass{article}
\newcommand{\foo}{}
\newcommand{\currentbar}{\thebar}
\newcounter{bar}
\setcounter{bar}{0}
\makeatletter
\newenvironment{baz}{\addtocounter{bar}{1}}{\g@addto@macro\foo{\currentbar}}
\makeatother
\begin{document}
\begin{baz}
\end{baz}
\begin{baz}
\end{baz}
\foo
\end{document}
I have a command named \foo, a counter named bar and an enviroment baz. I would like \foo to store the state of the counter bar at each end environment baz.
At the end of the first baz, bar equals 1, so 1 should be written to \foo. At the end of the second baz, bar equals 2 so \foo should be 12 (2 is concantenated to the already existing 1).
As is, the \foo is most likely actually \currentbar\currentbar, and that outputs 22 when called at the end of the document.
How could I "expand" the \currentbar to 1 and 2 so that \foo would be 12 at the end?
\\must be appended with\gapptoif one wants to create tabular data that way. – ipavlic Jun 07 '11 at 15:08\gapptois exactly the same as\g@addto@macroand thegstands for "global": environments form a group, so definitions inside them are lost when they are closed, unless the definition is global. If you trytexdef -t latex g@addto@macroyou'll see that this macro is defined with\xdef(global expanded definition). – egreg Jun 07 '11 at 15:18\\, but it was misformatted. – ipavlic Jun 07 '11 at 15:49\noexpandin front of it. This neutralizes the expandability during an\edefor\xdef. – egreg Jun 07 '11 at 15:51