Command to make comma separated commands
This doesn't check for list length, but that can be done. Likewise, the arguments can be added to a \foreachitem
loop.
\documentclass{article}
\usepackage{listofitems}
\newcommand\test[1]{%
\readlist*\mylist{#1}%
\mylist[1] is \mylist[2] a \mylist[3].
}
\begin{document}
\test{This, just, test}
\end{document}
Here's an example where number of arguments may vary, yet be accounted for:
\documentclass{article}
\usepackage{listofitems}
\newcommand\test[1]{%
\readlist*\mylist{#1}%
There are \mylistlen{} arguments:
\foreachitem \z \in \mylist[]{\ifnum\zcnt=1\relax\else, \fi(\zcnt) \z}.
}
\begin{document}
\test{This, just, test}
\test{This, just, test, today, Hallelujah}
\end{document}
I figured it out eventually:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\newcommand{\newcscommand}[3]{
\NewDocumentCommand{ #1 }{ >{\SplitArgument{#2 - 1}{,}}m }{\csname aux\cs_to_str:N #1\endcsname##1}
\expandafter\newcommand\csname aux\cs_to_str:N #1\endcsname[#2]{#3}
}
\ExplSyntaxOff
\newcscommand{\test}{3}{#1 is #2 a #3.}
\begin{document}
\test{This, just, test}
\end{document}
Your idea is good, but proper expl3
programming is better:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newcscommand}{mO{0}m}
{
\NewDocumentCommand{#1}{ >{\SplitArgument{#2-1}{,}}m }
{
\use:c { __noibe_\cs_to_str:N #1:\prg_replicate:nn{#2}{n} } ##1
}
\cs_new:cn { __noibe_\cs_to_str:N #1:\prg_replicate:nn{#2}{n} } { #3 }
}
\ExplSyntaxOff
\newcscommand{\test}[3]{#1 is #2 a #3.}
\begin{document}
\test{This, just, test}
\end{document}
Note that the syntax of \newcscommand
is similar to \newcommand
.
The trick is indeed to create a macro with the correct number of arguments in its signature.
A variant that avoids calling \use:c
and \prg_replicate:nn
each time \test
is executed, because the control sequence name is built at definition time thanks to \exp_not:c
.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newcscommand}{mO{0}m}
{
\exp_args:Nnne \NewDocumentCommand{#1}{ >{\SplitArgument{#2-1}{,}}m }
{
\exp_not:c { __noibe_\cs_to_str:N #1:\prg_replicate:nn{#2}{n} } ##1
}
\cs_new:cn { __noibe_\cs_to_str:N #1:\prg_replicate:nn{#2}{n} } { #3 }
}
\exp_args_generate:n { nne }
\ExplSyntaxOff
\newcscommand{\test}[3]{#1 is #2 a #3.}
\begin{document}
\test{This, just, test}
\end{document}