[texhax] FW: Re: Defining a command which runs differently based ion input argument
Uwe Lueck
uwe.lueck at web.de
Tue Dec 28 12:49:19 CET 2010
[and failed to send the correction to the list. this is not really UMTS, and sent from no iPad -- U.]
-----Ursprüngliche Nachricht-----
Gesendet: 28.12.2010 12:34:37
An: mb78aa at yahoo.com, "Lars Madsen" <daleif at imf.au.dk>
I wrote:
> \newcommand\mycommand[1][\vspace{1cm}]{} % or \vsapce?
sorry, must read
\newcommand\mycommand[1][\vspace{1cm}]{#1}
I stopped proof-reading when Paul's answer made "bling".
What I told about \@ifnextchar is what Paul did in plain TeX.
And I had to type this correction a second time because my UMTS connection failed while sending.
Cheers,
Uwe.
-----Ursprüngliche Nachricht-----
Von: "Uwe Lueck"
Gesendet: 28.12.2010 12:00:24
An: mb78aa at yahoo.com, "Lars Madsen" <daleif at imf.au.dk>
Betreff: Re: [texhax] Defining a command which runs differently based ion input argument
"Lars Madsen" <daleif at imf.au.dk>, 28.12.2010 10:43:04:
>Mike B. wrote:
>>
>> 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
>>
>> Season's greetings,
>> Mike.
>
> that is sadly not possible. You cannot make macros that can run as
> \macro and \macro{...}
?
First, if you had no idea about LaTeX, wouldn't you say that the question is about so-called "optional" arguments and "default" settings?
Now, if you type `vsapce', I guess you must have had a look at LaTeX code once in your life, if only unconsciously and without knowing what LaTeX is.
You may then dare to try a second encounter with LaTeX. In some documentation you may find how wonderfully you can define commands with optional arguments with LaTeX, using the -- --- ---- optional argument of \newcommand ... ..... ....... ! !! !!!
The only trouble with the present question is, that using this idea, you must sacrifice the idea to use curly braces for the optional argument. How important is it to have the optional argument in braces instead of square brackets, as with LaTeX?
\newcommand\mycommand[1][\vspace{1cm}]{} % or \vsapce?
Then try \mycommand and \mycommand[xyz]
General scheme: \newcommand\mycommand[1][DEFAULT]{...}
> the {} are required in both cases (or at least eting a token is required
> in both cases)
Internal LaTeX wizardry even allows optional arguments in curly braces:
\makeatletter
\newcommand\mycommand{\@ifnextchar\bgroup{\mycommandoptarg}{\vspace{1cm}}}
\newcommand\mycommandoptarg[1]{#1} % or \let\mycommandoptarg\@firstofone in this case
\makeatother
Inherent problems: you may type a brace for another reason and don't realize that it will be considered the start of an optional argument.
That is much worse with LaTeX's square brackets for optional arguments, as some scholars use square brackets for ellipsis or redactional insertions (I experienced this several times):
Title-like\\ % imitating the manuscript's line break and spacing
[unreadable] ipsum dolor sit amet
Cheers,
Uwe.
> as for testing to see if #1 is empty I recommend usinf the etoolbox
> package the \ifblank{#1}{empty}{not empty} is very useful
More information about the texhax
mailing list