[XeTeX] newcommand with optional arguments

Manuel Pégourié-Gonnard mpg at elzevir.fr
Tue Mar 4 01:36:05 CET 2008


Hi,

Ross Moore a écrit :
> In fact this is not correct.
> There are at least 2 problems with this coding.
> 
I agree with you on this point. \ifx#1\empty isn't the correct way to do.

> Better coding (why?) would be:
> 
>         \ifx\empty#1\empty
>            \typeout{no 1st argument for \string\nuu{#2}}%
>         \else
> 
> 
> But this still gets it wrong on things like:
> 
>     \nuu[\empty and then some]{this 3}
> 
True, but in real life, user-supplied string beginnig with \empty is quite
rare. Even more if you use \@empty instead. For me, the real problem of this
approach is if #1 possibly contains something looking like unbalanced \if's to
TeX.

> A more bullet-proof approach is:
> 
> \makeatletter
> \def\myempty@{}
> \makeatother
> \let\testtheargument\relax
> 

\makataletter here ? To use \myempty at ...
> \newcommand{\nuu}[2][]{%
>    \def\testtheargument{#1}%
>    \textit{#2}%
>    \expandafter\ifx\expandafter\myempty@\testtheargument\myempty@

Using \def and the expandafters is equivalent to using just #1, with to
problems added :
- #1 should not contain any # now;
- the test becomes non fully expandable;
(which should not be a problem in this case, however).

If you choose to use a \def, the simpler and best solution is
\def\arg at one{#1}
\ifx\arg at one\@empty
  \ETC.

> But even this has the side-effect of leaving \testtheargument
> with a value different from what it was before \nuu is encountered.
> 
This should not be a problem, since \testtheargument should be used only for this.

> This technicality is overcome using the following:
> 
Useless, IMHO.

> And this may not be the end of the story; but enough for now.
> 
Well, if you want to test wether an argument is empty or not, the best is to
use the ifmtarg package : it's reliable (no problems with \ifs or string
beginning with \empty) and fully expandable (this can be important). The only
particularity is that one string consisting only of space tokens is considered
empty.

By the way, all this discussion has little (if at all) to do with XeTeX, and
would surely be more natural in comp.text.tx or so...

Manuel.


More information about the XeTeX mailing list