What is rule in specifying bracket position:
\foo[]{} %or
\foo{}[]
Following syntaxes are clear, is there any other combinations?
\foo{}{}
\foo{}
...
What is rule in specifying bracket position:
\foo[]{} %or
\foo{}[]
Following syntaxes are clear, is there any other combinations?
\foo{}{}
\foo{}
...
The xparse package allows you to easily specify the type and order of the parameters supplied to an argument. In terms of optional arguments with square brackets [] (most common), use o or O{<default>}, while a mandatory argument is specified using m. So, for example,
\foo[]{}:\NewDocumentCommand{\foo}{o m}
\foo{}[]:\NewDocumentCommand{\foo}{m o}
\foo{}{}:\NewDocumentCommand{\foo}{m m}
xparse allows any combination of the above (to a maximum of 9 arguments) and intermixed versions thereof. This mostly depends on the application or intended uses. For example, the wrapfig package provides the wrapfig environment that takes arguments of the following form (using xparse notation): o m o m.
Using more than one consecutive optional argument is typically avoided, since you can't specify the latter without specifying the former, making them somewhat non-optional and therefore confusing.
Producing optional arguments the "traditional" way is achieved via "helper macros" - additional macros that are executed depending on the type of argument(s) supplied. This allows for the argument between [] to be considered optional:
\def\mycmd{\@ifnextchar[{\@with}{\@without}}
\def\@with[#1]#2{hello #1, have you met #2?}
\def\@without#1{goodbye #1}
The above shorthand followed from Different command definitions with and without optional argument.
xparse you can get away with multiple optionals and avoid some confusion using different delimiters, e.g. \NewDocumentCommand {\foo} { d<> d() d[] }
– Scott H.
Jul 11 '12 at 06:21
[] for optional arguments except in special cases () for picture mode coordinates and <> for package-specific syntax such as beamer. If you ned a lot of options it is better to use a single [] argument and then a key=value syntax within that.
– David Carlisle
Jul 11 '12 at 08:58
\sqrt[3]{2}, and the[]represent an optional parameter. The third is the normal case of two parameters as in\frac{}{}. The second, having the optional parameter following the mandatory is possible using thexparsepackage but I would recommend not doing that. – Peter Grill Jul 11 '12 at 05:13