I'd like to define a new command with optional parameters using the xparse
package. Please consider the following example:
\documentclass{minimal}
\usepackage{xparse}
\DeclareDocumentCommand{\mycommand}{ O{mydefault} m o o o }{%
p:#2%
\IfNoValueTF{#3}%
{}%
{ p:#3}%
\IfNoValueTF{#4}%
{}%
{ p:#4}%
\IfNoValueTF{#5}%
{}%
{ p:#5}
p:#1
}
\begin{document}
\mycommand[one]{two} \par % p:two p:one
\mycommand[one]{two}[three] \par % p:two p:three p:one
\mycommand[one]{two}[three][four] \par % p:two p:three p:four p:one
\mycommand[one]{two}[three][four][five] \par % p:two p:three p:four p:five p:one
\mycommand[one]{two}[three][][five] \par % p:two p:three p: p:five p:one
\mycommand[one]{two}[][][five] % p:two p: p: p:five p:one
\end{document}
The problem now is that when I'm leaving some parameters empty between two others that are filled in, LaTeX also displays those empty parameters. Applied to the last example I'd like to get p:two p:five p:one
.
[]
is different from an optional argument which is missing. However, I'd point out that runs of optional arguments don't really make for good interface design in most cases. What is the broader context here?Art. 1 Abs. 2 Ziff. 3 Bst. a XYZ
,Art. 1 Bst. a XYZ
, ...). And as I'm a lazy guy, it's easier just to write\art{1}[2][3][a]
. But if you know a better solution how to handle this please tell it to me.