Is it possible to fully expand tokens in an expandable manner and preserves category codes? I'd like to do this just using pdfTeX.
I'm looking for something that would work like this:
\def\foo{foo}
\fullyexpand{\foo bar baz}
which would expand to the 10 tokens "foobar baz" and retain the category codes.
If we drop the expandable requirement, then this is easy.
\def\fullyexpand#1{\edef\fetemp{#1}\fetemp}
If we drop the retain catcode requirement, it's doable, but less straight-forward. Here's the best I could come up with.
\def\gobbleprefix#1#2\femarker{%
\ifnum\escapechar<0
#1%
\else\ifnum\escapechar>255
#1%
\fi\fi
#2%
}
\def\fullyexpand#1{%
\expandafter\expandafter\expandafter\gobbleprefix
\expandafter\string\csname#1\endcsname\femarker
}
I was hoping to combine the ε-TeX extension \scantokens
with the above (by an appropriate, trivial modification of \gobbleprefix
), but of course, that does not work. \scantokens
's expansion is empty and pdfTeX just acts like it has opened a new file.
(As an aside, it amuses me that not expanding tokens is expandable—using \unexpanded
—but expanding tokens isn't, at least not obviously so.)
\expandsome{\foo{ } \bar{\expandthis\foo}\expandthis\bar{abc}}
which will expand precisely the tokens following\expandthis
, in an expandable manner. The code can be found online, or at this question. --- Actually, it would help me if someone has a list of all primitives that should be expanded.