[Tex/LaTex] How to specify an optional final argument with \newcommand

macrospackage-writing

I'm writing my first package, and I'm trying to work out how to create a new command with an optional final argument. In this instance, if an argument is omitted, it defaults to blank. This is problematic because it assumes that the two arguments passed in this case are #2 and #3.

\newcommand{\sfsx}[3][]{\wrapper{#1}{#2}{#3}}

Unfortunately, I would like it the other way around, where if only two arguments are given, they are assumed to be #1 and #2. Is there a simple way to do this that doesn't invoke other packages?

(\wrapper in this case is just another command that takes on various forms depending on package options).

Best Answer

Try the new xparse package, which is built on top of LaTeX3. With this package you can specify arbitrary combinations of mandatory and optional arguments. This case would be as easy as

\DeclareDocumentCommand \sfsx { m m o } { \wrapper{#1}{#2}{#3} }

If you want to have your own default value, use O{value} instead of o.