Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

What is rule in specifying bracket position:

\foo[]{} %or 
\foo{}[]  

Following syntaxes are clear, is there any other combinations?

\foo{}{} 
\foo{} 
...
share|improve this question
    
Assuming you are asking about parameters to macros: The first and last are the usual uses. An example of the first is \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 the xparse package but I would recommend not doing that. –  Peter Grill Jul 11 '12 at 5:13
    
there is no general rule, it depends on what the developer had in mind. Any number and any combination of parameters are possible. –  Herbert Jul 11 '12 at 5:42
add comment

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
With 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 6:21
3  
@ScottH. You could but you shouldn't use delimiters just to distinguish arguments. LaTeX always uses [] 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 8:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.