[texhax] Defining a command which runs differently based ion input argument

Philip Taylor (Webmaster, Ret'd) P.Taylor at Rhul.Ac.Uk
Tue Dec 28 12:28:03 CET 2010



Mike B. wrote:
> Hi all,
>
> How can I define a command which runs differently based on if an input argument was provided or not?.

Maybe something along the lines of the following, Mike :

\let \bgroup = { % Redundant; added solely for clarity

\def \mycommand {\futurelet \nexttoken \branchoncatcode}

\def \branchoncatcode
     {\ifcat \noexpand \nexttoken \noexpand \bgroup
         \let \next = \mycommandwithparameter
      \else
         \let \next = \mycommandwithoutparameter
      \fi
      \next
     }

\def \mycommandwithparameter #1{#1}
\def \mycommandwithoutparameter {\vskip 1 cm \relax}

% tests start here

\mycommand
\mycommand {Hello}
\mycommand
\mycommand {Goodbye}
\mycommand
\mycommand %
\end

Philip Taylor


More information about the texhax mailing list