# \aftergroup a list of tokens

\aftergroup executes the next token after the end of the current group. Is there a variant, \Vaftergroup{\some\tokens\a\b} that would put \some\tokens\a\b after the current group ends? Of course, one can do \aftergroup\some\aftergroup\tokens\aftergroup\a\aftergroup\b, but when the list is longer, it becomes messy.

This is motivated by an answer that works but is ugly.

-
 mark wooding's doafter package was designed after he had asked this question some time in the 80s. it's conceivable he did it without benefit of etex (which would have been very new at the time, if it existed at all) – wasteofspace May 14 at 9:21

In much the same vein as Herbert's answer, a 'pre-built' version of the same idea is provided by the etextools package: see \AfterGroup. I guess that \aftergroup is used rarely enough that this has not been a big demand :-)

-
studying this package will teach me much about the fine points of expansion, I believe :). Thanks for the info! – Bruno Le Floch Jan 2 '11 at 23:24
@Bruno ...or else drive you mad :) – Will Robertson Jan 3 '11 at 2:04
The \AfterGroup macro solves the problem in the way outlined by Herbert, but defining a new global temporary macro each time (via a counter) which is then undefined at use time. – egreg May 14 at 9:21

don't know if this might help.

\documentclass{article}

\def\ta{Hello}
\def\tb{ World. }
\def\tc{How are you?}
%\def\Tokens{\ta\tb\tc}

\begin{document}

\begingroup
\gdef\Tokens{\ta\tb\tc}% or like above with def
\aftergroup\Tokens
\let\ua=\tc
\let\ub=\tb
\let\uc=\ta
\ua\ub\uc---\par
\endgroup

\end{document}

-
Thanks. This is what I ended up doing, but I didn't like the idea of a global temporary variable: bad things can happen before the group is finished. – Bruno Le Floch Jan 2 '11 at 22:55

Below there is an alternative. \Tokens remains local.

\documentclass{article}

\def\ta{Hello}
\def\tb{ World. }
\def\tc{How are you?}
%\def\Tokens{\ta\tb\tc}

\begin{document}

\begingroup
\def\Tokens{\ta\tb\tc}% or like above with def
\let\ua=\tc
\let\ub=\tb
\let\uc=\ta
\ua\ub\uc---\par
\expandafter\endgroup\Tokens

\show\Tokens
\end{document}

-
 This requires that you 'know' where the end-of-group is (and is going to be tricky to pull off in box/\halign contexts). Herbert's answer is 'general' in the sense you don't need to know where or how the group is going to end. – Joseph Wright♦ May 14 at 9:13