Use case: define a command that loops over a list of input names, defining a command from each name.
In the working code below, note the \expandafter\definput\expandafter{\x} within a loop with \x as the loop variable.
The necessity is clear: we need to expand \x to each value in the loop range before expanding \definput on each one.
As the programmer, I wanted to say "Please substitute the loop value for \x everywhere before expanding the rest of the loop body."
To do so is non-intuitive, requiring an explicit "Wait to expand me" on every other macro in the loop body that ends up consuming the loop variable.
To complicate this, the tool to delay expansion is \expandafter, which only operates on the following 2 tokens, so you need to keep chaining \expandafter until you reach the loop variable.
Written functionally, the solution is: expandafter('\definput', expandafter('{', '\x')) (followed by normal token '}').
This expands \x to each iteration's value, then { to itself, then \definput, consuming {, the iteration value, and }.
Coming from other programming languages where you can easily group things together, this input chaining feels like pulling teeth.
Questions:
- Is there any way to group together the
\definputand{to say "don't expand this group" with one\expandafter? - Is this problem fundamental to LaTeX's token-processing design?
\documentclass[11pt]{article}
\usepackage{tikz}
% On input x, define \x as "the definition of x"
\newcommand{\definput}[1]{%
\expandafter\gdef\csname#1\endcsname{the definition of #1}%
}
\newcommand{\defallinputs}[1]{\foreach \x in {#1} {\expandafter\definput\expandafter{\x}}}
% Define \x and \y
\defallinputs{x, y}
\begin{document}
\x\par\y
\end{document}
becomes:
the definition of x
the definition of y
For reference, the use case that inspired this question is: Using \xdef in \foreach errors when using another \foreach inside the \xdef


\expandafteris the heart ofexpl3to if\foohas two arguments and you want to fully expand the second before calling\foouse\ExpandArgs{ne}\foo{abc}{\qqq}– David Carlisle Nov 27 '22 at 20:33