UK TeX FAQ -- question 236

### A command with two optional arguments

If you've already read "breaking the 9-argument limit" you can probably guess the solution to this problem: command relaying.

LaTeX allows commands with a single optional argument thus:

      \newcommand{\blah}[1][Default]{...}

You may legally call such a command either with its optional argument present, as `\blah[nonDefault]` or as `\blah`; in the latter case, the code of `\blah` will have an argument of `Default`.

To define a command with two optional arguments, we use the relaying technique, as follows:

      \newcommand{\blah}[1][Default1]{%
        \def\ArgI{{#1}}%
        \BlahRelay
      }
      \newcommand\BlahRelay[1][Default2]{%
        % the first optional argument is now in
        %   \ArgI
        % the second is in #1
        ...%
      }

Of course, `\BlahRelay` may have as many mandatory arguments as are allowed, after allowance for the one taken up with its own optional argument - that is, 8.

A command with two optional arguments strains the limit of what's sensible: obviously you can extend the technique to provide as many optional arguments as your fevered imagination can summon. However, see the comments on the use of the keyval package in "breaking the 9-argument limit", which offer an alternative way forward.

An alternative approach is offered by Scott Pakin's newcommand program, which takes a command name and a definition of a set of command arguments (in a fairly readily-understood language), and emits (La)TeX macros which enable the command to be defined. The command requires that a python system be installed on your computer.

`newcommand`
  support/newcommand.tar.gz