[XeTeX] newcommand with optional arguments

Ross Moore ross at ics.mq.edu.au
Mon Mar 3 22:41:34 CET 2008


Hi again,

On 04/03/2008, at 8:30 AM, Ross Moore wrote:


> A more bullet-proof approach is:
>
> \makeatletter
> \def\myempty@{}
> \makeatother
> \let\testtheargument\relax
>
> \newcommand{\nuu}[2][]{%
>   \def\testtheargument{#1}%
>   \textit{#2}%
>   \expandafter\ifx\expandafter\myempty@\testtheargument\myempty@
>    \typeout{no 1st argument for \string\nuu{#2}}%
>   \else
>    \ `#1'
>   \fi
> }

Actually the \expandafter s  are not needed here.
It should be just:


\newcommand{\nuu}[2][]{%
   \def\testtheargument{#1}%
   \textit{#2}%
   \ifx\testtheargument\myempty@
    \typeout{no 1st argument for \string\nuu{#2}}%
   \else
    \ `#1'
   \fi
}


>
> But even this has the side-effect of leaving \testtheargument
> with a value different from what it was before \nuu is encountered.
>
>
> This technicality is overcome using the following:
>
>
> \newcommand{\nuu}[2][]{%
>  \begingroup
>   \def\testtheargument{#1}%
>   \textit{#2}%
>   \expandafter\ifx\expandafter\myempty@\testtheargument\myempty@
>    \typeout{no 1st argument for \string\nuu{#2}}%
>   \else
>    \ `#1'
>   \fi
>  \endgroup
> }

\newcommand{\nuu}[2][]{%
  \begingroup
   \def\testtheargument{#1}%
   \textit{#2}%
   \ifx\testtheargument\myempty@
    \typeout{no 1st argument for \string\nuu{#2}}%
   \else
    \ `#1'
   \fi
  \endgroup
}



>
>
>
>> S.
>
> And this may not be the end of the story; but enough for now.
>

Hope this helps,

	Ross

------------------------------------------------------------------------
Ross Moore                                         ross at maths.mq.edu.au
Mathematics Department                             office: E7A-419
Macquarie University                               tel: +61 +2 9850 8955
Sydney, Australia  2109                            fax: +61 +2 9850 8114
------------------------------------------------------------------------




More information about the XeTeX mailing list