Testing whether an optinal argument is supplied

25 Sep 2008

In LaTeX, you want to test whether a macro was called with or without an optional argument.

Although there are many strategies to test if an optional parameter is present, my preferred method uses etoolbox for simplicity and readability:

\usepackage{etoolbox}

\newcommand\mycmd[2][]{%
  \ifstrempty{#1}{%
    % something with #2
  }{%
    % some other thing with #1 and #2
  }%
}