Expansion is often cited as one of the most arcane aspects of TeX, more akin to witchcraft than to something easily picked up by the newcomer. There are many great questions and great answers about expansion on the site but, although I like to think I'm getting better at it, I still find myself stumped by more complicated cases.
For instance, take the LaTeX internal called \in@
. Martin Scharrer's excellent List of internal LaTeX2e macros explains \in@
as follows:
\in@{⟨1⟩}{⟨2⟩}
Checks if first argument occurs in the second and sets the switch
\ifin@
accordantly. The arguments are not expanded. This must be done beforehand.
Because I'd like to pass once-expandable macros as arguments to \in@
, I need to expand each of those arguments once before \in@
gets expanded.
I managed to handle the case where the first argument needs to be expanded once and the second argument doesn't need expansion (see my code below).
However, I can't get my head around the \expandafter
juggling required to handle the case in which both arguments need to be expanded once. I think (correct me if I'm wrong) that I have to leave the braces untouched until \in@
itself is processed, and that's what I find difficult. I'd appreciate your help on this particular expansion problem.
\documentclass{article}
\let\ex=\expandafter % <--- more readable \expandafter
\begin{document}
\makeatletter
\in@{foo}{foo,bar,baz} % <--- works as expected
\ifin@ true\else false\fi
\def\foo{foo}
\ex\in@\ex{\foo}{foo,bar,baz} % <--- so far, so good...
\ifin@ true\else false\fi
\def\cslist{foo,bar,baz}
\in@{\foo}{\cslist} % <--- What combination of \expandafter is needed here?
\ifin@ true\else false\fi
\makeatother
\end{document}
However, more generally, I'd like to know what general procedure expansion gurus follow in their head (and possibly on paper) to expand a sequence of tokens in the desired order. Stephan v. Bechtolsheim's A tutorial on \expandafter
gave me some insight, but I'm still far from mastering it. I think sharing your tricks and recipes might help me and others improve our expansion skills.
So, what cognitive processes allow you to bend \expandafter
to your will?
Note: I insist on using only \expandafter
here, and none of that \edef
/ \noexpand
trickery. :)
:-)
\exp_args:No
and similar, which expands to a sequence of\expandafter
and obviously much easier to use.