Optional arguments like \section

Optional arguments, in macros defined using \newcommand, don’t quite work like the optional argument to \section. The default value of \section’s optional argument is the value of the mandatory argument, but \newcommand requires that you ‘know’ the value of the default beforehand.

The requisite trick is to use a macro in the optional argument:

  optional arg: #1,  mandatory arg: #2%
\thing{manda}% #1=#2

\thing[opti]{manda}% #1="opti"

LaTeX itself has a trickier (but less readily understandable) method, using a macro \@dblarg; inside LaTeX, the example above would have been programmed:

  optional arg: #1,  mandatory arg: #2%

In that code, \@thing is only ever called with an optional and a mandatory argument; if the default from the \newcommand is invoked, a bug in user code has bitten…

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=oarglikesect