[texhax] Defining a command which runs differently based ioninput argument
Ulrike Fischer
news3 at nililand.de.org
Tue Dec 28 11:02:48 CET 2010
Am Tue, 28 Dec 2010 10:43:04 +0100 schrieb Lars Madsen:
> Mike B. wrote:
>> 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
> that is sadly not possible. You cannot make macros that can run as
> \macro and \macro{...}
Sure it is possible. The LaTeX-kernel defines \input differently
depending on if a brace follows or not:
\def\input{\@ifnextchar\bgroup\@iinput\@@input}
You only need to copy this:
\documentclass[12pt]{article}
\begin{document}
\makeatletter
\def\mymacro{\@ifnextchar\bgroup\@mymacroi\@mymacroii}
\def\@mymacroi#1{BLA -- \textbf{#1} -- BLA }
\def\@mymacroii{blubb -- }
\mymacro \mymacro{abc}
\end{document}
--
Ulrike Fischer
More information about the texhax
mailing list