[comp.text.tex] Testing for an empty argument in a definition

horstman@sjsumcs.sjsu.edu (Cay Horstmann) (01/02/91)

I would like to define a TeX macro which reacts differently when called
with a blank vs. a non-blank argument. Specifically, I want
     \mymacro{(1)}  ==> \eqno{(1)}
     \mymacro{}     ==> {}
How do I test whether the macro argument #1 is empty? In the ever-helpful
TeXbook, there is a \ifvoid <number> command which tests whether register
<number> is void (whatever that is), but which registers are available for
a lousy macro like mine? I don't want to mess anything up. (It never ceases
to amaze me how TeX violates every principle of software engineering known to 
man. For crying out loud, has Don Knuth never heard of namespace control? )

Anyway, what is the magic incantation to test for emptiness? Just email,
I doubt that the teeming TeX thousands are interested in this. Thanx for 
your help!

Cay

kesich@acf3.NYU.EDU (John Kesich) (01/04/91)

%  (Might I guess that the "\mymacro{}" is being automatically
%   generated and that getting TeX to deal with a null argument
%   is easier than having the generating software surpress the
%   call when the argument is null?)
%
%  Knuth does provide commands for managing the namespace, see:
%  \newbox, \newcount, \newdimen, etc.
%
%  At any rate, sounds like you want something like:
\def\mymacro#1{%
    \def\Tmp{#1}%
    \ifx\Tmp\empty
    \else
        \eqno{#1}%
    \fi
}
$$ a = b + c \mymacro{(1)} $$
$$ x = y + z \mymacro{} $$
\bye

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (01/04/91)

horstman@sjsumcs.sjsu.edu (Cay Horstmann) writes:

>I would like to define a TeX macro which reacts differently when called
>with a blank vs. a non-blank argument. Specifically, I want
>     \mymacro{(1)}  ==> \eqno{(1)}
>     \mymacro{}     ==> {}
\def\null{} %this one is given in the plain format, I just include
            % it for completeness
\def\mymacro#1{\def\test{#1}\ifx\test\null {}\else \eqno{#1}\fi}

\ifx is very handy for all sorts of equality tests.

>In the ever-helpful
>TeXbook, there is a \ifvoid <number> command which tests whether register
><number> is void (whatever that is), 

That's box registers. Won't help you on this one.

>(It never ceases
>to amaze me how TeX violates every principle of software engineering known to 
>man. For crying out loud, has Don Knuth never heard of namespace control? )

TeX has its own logic. But it's not quite clear to me what you're
fulminating against here.

Victor.

art@hpclpa.HP.COM (Arthur Ogawa) (01/05/91)

The following macro definition will do what you want:

\def\mymacro#1{%
 \def\tempa{#1}\ifx\tempa\empty\eqno{#1}\else{}\fi
 }%

Note that PLAIN.TEX defines \empty as follows:

\def\empty{}%

LaTeX defines \@empty in a similar way, if you want to work with .sty files