[Tex/LaTex] How define LaTeX command with 2 or 3 arguments in comma-separated list

comma-separated listmacrosoptional argumentsxparse

To avoid typing subscripts and extra parentheses and brackets, I want to define a command, say \disk, that may be used with a comma-separated list of either 2 or 3 arguments, like this:

$\disk{r,x}$
$\disk{r,x,d}$

that will produce, respectively, the same output as would directly typing:

$D_{r}(x)$
$D_{r}(x;d)$

[The r, x, and d could, of course, by any letter or number, etc. For example, I might use \disk{\epsilon, 0, D}, etc.]

Using xparse or otherwise, how might this be done?

Although I am aware that the two different outputs could be obtained by using an optional argument, that would require typing the 3rd, optional argument inside brackets; and in any case even for the case of only two arguments, I want to speed up typing by using just the comma-separated list {x, d} rather than {x}{d}.

Best Answer

We can do this at TeX primitive level without any xparse or another package. Note, there are only two lines of code.

\def\disk#1{\diskA#1,,\end}
\def\diskA#1,#2,#3,#4\end{D_{#1}\ifx,#3,(#2)\else (#2;#3)\fi}

$\disk{r,x}$

$\disk{r,x,s}$

\end

If you need to do something like this $\disk{\epsilon,(1,2),\rho}$ then add one line of code:

\def\disk#1{\diskA#1()\end}
\def\diskA#1(#2)#3\end{\ifx\end#3\end\diskB#1,,\end \else\diskA#1{(#2)}#3\end \fi}
\def\diskB#1,#2,#3,#4\end{D_{#1}\ifx,#3,(#2)\else (#2;#3)\fi}

$\disk{r,x}$

$\disk{r,x,s}$

$\disk{\epsilon,(1,2),\rho}$

\end