In LATEX, commands can have both mandatory and optional arguments,
for example in:
\documentclass[11pt]{article}
the |11pt| argument is optional, whereas the |article| class name is
mandatory.
In LATEX 2.09 users could define commands with arguments, but these
had to be mandatory arguments. With LATEX2e, users can now define
commands and environments which also have one optional argument.
These commands have a new, second, optional argument; this is used for
defining commands which themselves take one optional argument. This
new argument is best introduced by means of a simple (and hence not
very practical) example:
\newcommand{\example}[2][YYY]{Mandatory arg: #2;
Optional arg: #1.}
This defines || to be a command with two arguments, referred
to as |#1| and |#2| in the definition—nothing new so far. But
by adding a second optional argument to this | | (the
|[YYY]|) the first argument (|#1|) of the newly defined command
|| is made optional with its default value being |YYY|.
Thus the usage of || is either:
\example{BBB}
which prints:
Mandatory arg: BBB;
Optional arg: YYY.
or:
\example[XXX]{AAA}
which prints:
Mandatory arg: AAA;
Optional arg: XXX.
The default value of the optional argument is YYY.
This value is specified as the default argument of the
| | that created ||.
As another more useful example, the definition:
\newcommand{\seq}[2][n]{\lbrace #2_{0},\ldots,\,#2_{#1} \rbrace}
means that the input |
a| produces
the formula
{a0,…, an},
whereas the input |
[k]x| produces the formula
{x0,…, xk}.
In summary, the command:
| | cmd num default definition
defines cmd to be a command with num arguments, the first of
which is optional and has default value default.
Note that there can only be one optional argument but, as before,
there can be up to nine arguments in total.
LATEX2e also supports the creation of environments that have one
optional argument. Thus the syntax of these two commands has
been extended in the same way as that of | |.
This takes the same arguments as | |. If cmd is already
defined then the existing definition is kept; but if it is currently
undefined then the effect of |is to define cmd
just as if | | had been used.