My \john command is defined like so:
\def\john{\DontExpandMe}
I would now like to repeatedly change its definition, to keep adding some extra stuff on the front.
\foreach\i in {ape,bat,cow,dog} {
\xdef\john{\i,\unexpanded{\john}}
\show\john
}
My intention is that the \show\john command at each iteration should result in:
\john=macro: -> \DontExpandMe.
\john=macro: -> ape,\DontExpandMe.
\john=macro: -> bat,ape,\DontExpandMe.
\john=macro: -> cow,bat,ape,\DontExpandMe.
\john=macro: -> dog,cow,bat,ape,\DontExpandMe.
That is to say, I would like \john to be "partially expanded" in some sense. But I can't make that work. I have tried the following
- If I use an
\xdef, then the whole command is expanded, including the\DontExpandMepart. - If I just use a
\gdef, then the\iis not expanded. - If I use
\xdefwith\unexpanded{...}around\john(as I have in my current code) then I get\john=macro: -> ape,\john.and\john=macro: -> bat,\john.and so on.
Here is my code.
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\def\john{\DontExpandMe}
\show\john
\foreach\i in {ape,bat,cow,dog} {
\xdef\john{\i,\unexpanded{\john}}
\show\john
}
\end{document}
etoolbox's\expandonceinstead of\unexpanded– cgnieder May 30 '13 at 09:08