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

Philipp Stephani st_philipp at yahoo.de
Tue Dec 28 11:56:10 CET 2010


Am 28.12.2010 um 10:28 schrieb Mike B.:

> 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

This kind of argument is supported by the xparse package:

\documentclass{minimal}
\usepackage{xparse}
\NewDocumentCommand\mycommand{g}{\IfValueTF{#1}{#1}{\vspace{1cm}}}
\begin{document}
test\par
\mycommand
test\par
\mycommand{abc}
test
\end{document}

The argument specifier "g" means "a grouped argument or nothing", and \IfValueTF tests whether a given optional argument was passed.


More information about the texhax mailing list