David Z David Z - 1 year ago 149
LaTeX Question

LaTeX: how to use a required arg as the default for an optional one?

I'm trying to create a LaTeX command with two arguments, one of them optional. Normally I'd do this as

\newcommand{\whatever}[2][default]{first #1 second #2}

is the default value for the first argument. But for this command I want the value of the second argument to be used as the default value for the first argument - that is, I want


to be equivalent to


Does anyone know how to do this? I can drop down to plain TeX if necessary.

Answer Source

The LaTeX kernel has an inbuilt method of doing this, although it's not widely used. Here's an example:

\newcommand\@foo[2][]{[1: #1, 2: #2.] }


Obviously, the \makeatletter commands can be dropped if you're doing this inside a .sty or .cls file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download