7

I am trying to make macro with variadic arguments, iterating over them. The final result is supposed to be:

\foreach[x]((var = \x )){foo}{bar}{baz}\null

evals to

var = foo var = bar var = baz

My naive guess is this one:

\def\Macro#1{\if \null#1 . \else ,\noexpand\Macro \fi}
\Macro foogg\null

I expected it to eval to ,,,,,., but it evals to ,oogg. Am I understanding \noexpand behavior wrong?

egreg
  • 1,121,712
KAction
  • 703

2 Answers2

9

Change \noexpand to \expandafter, that's what you need to "skip" that \fi. As well, as egreg points out, \if\null won't work. Either use \ifx, or change \null to \relax and hope it is not contained in your text. The reason why \relax will work is that it is unexpandable and \if takes it, instead of expanding. For the reason that \relax or \null might be used by someone else, a much safer option is to use a command that doesn't exist, like \thisIStoheczSdelimiter:

\def\Macro#1{\ifx\thisIStoheczSdelimiter#1 . \else ,\expandafter\Macro \fi}
\Macro foogg\thisIStoheczSdelimiter

As for working foreach cycles, look into pgffor package. Your code, IIRC, could be rewritten as

\foreach \x in {foo, bar, baz}{ var = \x, }
yo'
  • 51,322
8
\def\Macro#1{\if \null#1 . \else ,\noexpand\Macro \fi}
\Macro foogg\null

\def\xnull{\null}
\def\Macro#1{\def\tmp{#1}\ifx\tmp\xnull\else ,#1\expandafter\Macro \fi}
\Macro foogg\null

\bye

produces ,foogg from your original then ,f,o,o,g,g from the modified version, in your original the \noexpand makes \Macro act like \relax and so do nothing after the first call, so only f is ever taken as an argument.

The above is plain TeX, but if you are intending this for LaTeX you should really use a different syntax, one of the main aims of LaTeX is to give a unifying syntactic structure, and LaTeX commands never take a variable number of {} arguments. The LaTeX syntax would be a comma separated list within a single {} argument.

David Carlisle
  • 757,742