macros,optional-arguments – Using More Than One Optional Argument for \newcommand in LaTeX

macrosoptional arguments

The \newcommand macro allows to use an optional argument for the first parameter #1 with:

\newcommand{\mycommand}[3][defaultfor1]{blah blah blah}

Is it possible to have more than one option with \newcommand?

Best Answer

Try the LaTeX 3 package xparse. For example:

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\foocmd}{ O{default1} O{default2} m }{#1~#2~#3}
                            %     ⤷ #1        ⤷ #2    ⤷ #3
\begin{document}
  \foocmd{foo} \par
  \foocmd[nondefault1]{foo} \par
  \foocmd[nondefault2][notfoo2]{foo} \par
\end{document}

Result of above's LaTeX code

You may read the documents for more information.

Related Question