I've been banging my head against this for days. I have a macro which takes a macro name and its arguments. It then re-arranges the arguments a bit and then calls the passed macro with the re-arranged arguments and so ends like this:
#1{\arga}{\argb}{\argc}
where arga ... \argc
are the processed arguments. However. I need to have \arga ... \argc
fully expanded before the macro stored in #1
is called. I cannot for the life of me work this out after days of playing with \expandafter
, \noexpand
, etextools
etc. I can't use expl3
and I'd really prefer not to use etextools
but etoolbox
is available. Here is a MWE:
\documentclass{article}
\begin{document}
\def\x#1#2#3#4{%
\def\arga{#2}%
\def\argb{#3}%
\def\argc{#4}%
#1{\arga}{\argb}{\argc}}
\def\y#1#2#3{\detokenize{#1#2#3}}
\x\y{arg1}{arg2}{arg3}
\end{document}
This results in "\arga \argb \argc" but I want "arg1arg2arg3". \expandafter
will do this in \x
but not for three arguments in a row. etextools
has some macros to do this but I really want to avoid it (it clashes with etoolbox
in some ways and I must have etoolbox
). I couldn't get the etextools
macros to work even when I tried (\ExpandNextTwo
etc.)
UPDATE: I realised that in my case, the args can contain robust macros like:
\documentclass{article}
\usepackage{etoolbox}
\begin{document}
\def\x#1#2#3#4{%
\def\arga{#2}%
\edef\argb{\ifstrequal{#3}{arg2}{arg2}{}}%
\def\argc{#4}%
{\protected@edef\z{\noexpand#1{\arga}{\argb}{\argc}}\z}}
\def\y#1#2#3{\detokenize{#1#2#3}}
\x\y{arg1}{arg2}{arg3}
\end{document}
In which case (using egreg's answer as an example), it isn't fully expanded. As Joseph and egreg mention below, this isn't doable, you just have to use non-robust macros in such cases, for example. I'll let the question stand as it because it is informative.
\detokenize
?\def
s here are clearly just to illustrate the point, as you don't actually need them in the minimised case :-)