I want to define a command (not with \def
) the simplest possible way that it
- takes arguments separated by a comma,
- supports an optional argument (the first one).
maybe another solution with extra optional arguments which are not necessarily the firsts will be welcome in the future.
I want to have something like this: \mycmd[opt arg1, opt arg2]{arg3, arg4}
.
I already saw https://tex.stackexchange.com/a/29975/92620 and https://tex.stackexchange.com/a/2860/92620.
\documentclass{report}
\newcommand\mycmd[4][]{optional: #1\\ mandatory: #2 - #3 - #4}
\begin{document}
\mycmd[optional arg]{second arg,third arg,fourth arg}
\end{document}
Best Answer
The following example uses
\comma@parse
of packagekvsetkeys
to parse the comma separated argument lists.Remarks:
The list of optional arguments can be shorter, the omitted arguments gets default values.
\comma@parse
trims the arguments by removing leading and trailing space.\comma@parse
removes empty entries. A workaround for empty arguments is using\relax
or\empty
.