I am using the following generator to generate variadic macros:
% USAGE :: \VARIADIC{name}{start}{mid}{stop}
%
\newcommand{\VARIADIC}[4]{%
\expandafter\newcommand\csname GobbleNext#1Arg\endcsname[1]{%
#3##1\csname CheckNext#1Arg\endcsname%
}%
\expandafter\newcommand\csname CheckNext#1Arg\endcsname{%
\csname @ifnextchar\endcsname\bgroup{\csname GobbleNext#1Arg\endcsname}{#4\endgroup}%
}%
\expandafter\newcommand\csname #1\endcsname[2]{%
\begingroup#2##1#3##2\csname CheckNext#1Arg\endcsname%
}%
}
Essentially, it checks if there are more "arguments" -- tokens that start with \bgroup
i.e. \{
and then decides whether to expand recursively or halt.
I use it as below:
\VARIADIC {List} {[} {;} {]}
\VARIADIC {Sum} {(} {+} {)}
...
\begin{document}
\List {a} {b} {c} {d} {e}
\Sum {x} {y} {z}
\end{document}
I can also apply simple styles like:
\VARIADIC {Sum} {\textbf\bgroup(} {+} {)\egroup}
But I am not sure if this is the best / right way to do so. I use \ensuremath
a lot, and the following macro doesn't work -- I'm not sure why ...
\VARIADIC {Tuple} {\ensuremath\bgroup\langle} {,} {\rangle\egroup}
I want to be able to compose a \VARIADIC
macro with existing macros easily. Concretely, I am looking for a way to compose an arbitrary existing unary macro, say \XYZ
as below:
\VARIADIC {Test} {\XYZ\bgroup(} {,} {)\egroup}
\textbf\bgroup a\egroup
only works by accident;\ensuremath\bgroup a\egroup
doesn't work at all. The proper syntax is with braces around the argument. – egreg Sep 8 '19 at 8:07[]
not{}
No standard latex command takes a variable number of{}
arguments. – David Carlisle Sep 8 '19 at 8:40{}
so that the macros look like regular macros. I think it to make the macros work with[]
, I basically have to replace the\newcommand
s with\def
s – Saswat Padhi Sep 8 '19 at 8:51