Pass 3 optional arguments in newcommand

macrosoptional arguments

In a mathematical text which I write, I want to denote a certain number by $S(x;q,a)$. The number, as you may have guessed, depends on three parameters $x, q, a$. I would like to give this expression a shortcut, because maybe I will change my mind in the end and rather denote the number as $S(x;a,q)$ or something the like. Then I will not have to go all over the text and look for instances of this expression.

So I defined a new command in the preamble in the following way:

\newcommand{\Sc}[3]{S(#1; #2, #3)}

This works, i. e. if I now type \Sc{x}{q}{a}, then the desired output is displayed.
Most times when I use this command, the three parameters I give to the command will be x, q, and a in this order. Only sometimes other parameters will occur in the use of \Sc. So a shortercut seems possible.

Ideally, I would like to only type \Sc as a shortcut for \Sc{x}{q}{a}. But the use \Sc{a}{b}{c} has to remain valid so that I can pass other parameters to \Sc if needed.
In order to achieve this, I tried

\newcommand{\Sc}[3][x][q][a]{S(#1; #2, #3)}

But apparently, there is at most one optional argument permitted in \newcommand, as I learned. (If this is wrong, please correct me.)

Is there a way to have all three parameters of my new command optional and such that x, q, and a are the defaul values?

Edit: Apparently, I didn't make the point clear enough. What I would like to typeset are two things. One is $S(x;q,a)$ with these three parameters x, q, and a. The other is $S(_; _, _)$ where I do not know yet what the three parameters are. Both should be possible with the same command \Sc.
For displaying $S(x;q,a)$, I would like to type only \Sc. So, in a sense, the parameters x (as the first parameter), q (second), and a (third) should be default. For displaying $S(_; _, _)$ with a parameter-triple (_, _, _) different from (x,q,a), I would like to use the command with all three parameters. For example I would type $\Sc{x}{q}{b}$ in order to produce $S(x;q,b)$.

Best Answer

I see two ways to do it.

With xparse you can use \NewDocumentCommand to create a new command with multiple optional arguments. Once you get beyond two optional arguments the usage becomes a bit clumsy and a key-value interface may be the better solution. The clumsiness is because with three consecutive optional arguments, if you only want to specify the third you must also specify the first and second. If you only want to specify the second you must also specify the first. Otherwise, there is no way to know which of the three optional arguments you are specifying.

MWE using xparse:

\documentclass{article}
\usepackage{xparse} % Automatically included in recent LaTeX releases;
                    % load manually as here for older releases.

\NewDocumentCommand{\Sc}{ O{x} O{q} O{a} }{%
  S(#1; #2, #3)
}%

\begin{document}
\Sc

\Sc[w][y][z] % change all three arguments

\Sc[m]       % change only the first argument

\Sc[x][m]    % change only the second argument

\Sc[x][q][m] % change only the third argument
\end{document}

Another way to do it is to use the xargs package, which provides the \newcommandx macro which, in turn, also lets you specify optional arguments with default values but the syntax and usage is a bit different. You don't explicitly have to supply the default value for, say, the first two arguments if all you want to change is the third argument, but you still must specify empty placeholder delimiters for the missing arguments.

MWE using xargs:

\documentclass{article}
\usepackage{xargs}

\newcommandx*{\Sc}[3][1=x,2=q,3=a,usedefault]{%
  S(#1; #2, #3)
}%

\begin{document}
\Sc

\Sc[w][y][z] % change all three arguments

\Sc[m]       % change only the first argument

\Sc[][m]     % change only the second argument

\Sc[][][m]   % change only the third argument
\end{document}

There may be other more elegant solutions. With three consecutive optional arguments, you need to be careful about what may happen if the user omits any of the arguments.

Related Question