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

Paul Isambert zappathustra at free.fr
Tue Dec 28 11:36:20 CET 2010


Selon "Mike B." <mb78aa at yahoo.com>:

> Hi all,
>
> How can I define a command which runs differently based on if an input
> argument was provided or not?. For example:
>
> \mycommand
> would introduce a vsapce of 1cm
>
> whereas
> \mycommand{xyz}
> would print xyz
>
> So it's conceptually something likel:
> if input argument is missing then
>    run this code
> else
>    run this code
> endif


You can do this:

\def\mycommand{%
  \futurelet\tmp\Mycommand
  }

\def\Mycommand{%
  \ifx\tmp\bgroup
    \expandafter\mycommandwitharg
  \else
    \expandafter\mycommandwithoutarg
  \fi
  }

\def\mycommandwitharg#1{ ... }
\def\mycommandwithoutarg{ ... }

Braces determine whether what follows is an argument or not, i.e. \mycommand\TeX
and \mycommand{\TeX} don't produce the same results.

Best,
Paul


More information about the texhax mailing list