I want to set up a macro where the presence of an argument will be
significant, that is
\newcommand\fig[3]{
\if <argument #1 is null> do this etc.
\else etc.
\fi
}
\fig{}{}{this is a phrase}
is different from
\fig{2}{3}{this is a phrase}
How do I test to see if an argument is null? I tried
\csname the #1 \endcsname but that naturally didn't work since the
argument is not a counter, register etc..
--
Gregory Lampshire
Virginia Tech NeXT CC
Bradley Department of Electrical Engineering
gb...@rglnext.geol.vt.edu (NeXTmail welcome)
------------------------
My opinions are my very own and do not reflect the opinions of any
employer
I ran into the same problem some days ago. Here's a piece of the
code that tests whether the first argument is empty or not:
{\setbox0=\hbox{\ignorespaces#1\unskip\relax}%
\ifdim \wd0=0pt%
% do whatever you want if the argument is empty
\else%
% do whatever you want if it isn't
\fi%
}%
I'm not a TeXpert, but it works fine for me.
>
>
>--
>Gregory Lampshire
>Virginia Tech NeXT CC
>Bradley Department of Electrical Engineering
>gb...@rglnext.geol.vt.edu (NeXTmail welcome)
>------------------------
>My opinions are my very own and do not reflect the opinions of any
>employer
------
wszc...@mailhost.ecn.uoknor.edu
wit...@eis.cs.tu-bs.de
------
C: A middle-level programming language which combines the power
of assembly language with the readability of assembly language.
> I ran into the same problem some days ago. Here's a piece of the
> code that tests whether the first argument is empty or not:
> {\setbox0=\hbox{\ignorespaces#1\unskip\relax}%
> \ifdim \wd0=0pt%
> % do whatever you want if the argument is empty
> \else%
> % do whatever you want if it isn't
> \fi%
> }%
This works and is more general than the above solution (it does not
falsely declare an argument null if the argument produces no text.)
Almost certainly there are better solutions.
mike
\def\makeatletter{\catcode`\@=11\relax}
\def\makeatother{\catcode`\@=12\relax}
\makeatletter
\def\ifnullarg#1\ifnullarg#2#3{%
\begingroup
\def\@flag{\relax\relax\relax}
\def\@na@yes{#2}%
\def\@na@no{#3}%
\ifx\@flag#1\@flag\let\@na@chosen=\@na@yes
\else\let\@na@chosen=\@na@no\fi
\relax
\@na@chosen
\endgroup
}
\makeatother
% Testing -- Cut here --
\def\iw#1{\immediate\write16{#1}}
\def\usenullchk#1{%
\ifnullarg#1\ifnullarg{\iw{Null}}%
{\iw{Not Null}}}
\usenullchk{}
\usenullchk{foo}
\usenullchk{\relax}
\end
There are a number of ways to do this, but the following is the best.
(Best in most senses...but not in clarity!)
There are two types of syntax to check for null parameters:
\ifnull{#1}\then ... \else...\fi
\if\naught{#1}...\else...\fi
Choose the definitions that correspond to the desired syntax.
%========================================================================
% \ifblank --- checks if parameter is blank (Spaces count as blank)
% \ifgiven --- checks if parameter is not blank: like \ifblank\else
% \ifnull --- checks if parameter is null (spaces are NOT null)
% use \ifgiven{#1}\then ... \else ... \fi etc
\let\then\iftrue
{\catcode`\!=8 % funny catcode so ! will be a delimiter
\long\gdef\ifgiven#1\then{\Ifbl@nk#1@@@\empty!}% negative of \ifblank
\long\gdef\ifblank#1\then{\Ifbl@nk#1@@..!}% if null or spaces
\long\gdef\Ifbl@nk#1#2@#3#4!{\ifx#3#4}
\long\gdef\ifnull#1\then{\IfN@LL#1* {#1}!}% if null
\long\gdef\IfN@LL#1 #2!{\ifblank{#2}\then}
%
%
% or.... use \if\given, \if\blank, \if\naught (\null already exists)
% without \then.
%
\long\gdef\given#1{\fi\Ifbl@nk#1@@@\empty!}
\long\gdef\blank#1{\fi\Ifbl@nk#1@@..!}% if null or spaces
\long\gdef\naught#1{\IfN@Ught#1* {#1}!}% if null
\long\gdef\IfN@Ught#1 #2!{\blank{#2}}
\long\gdef\Ifbl@nk#1#2@#3#4!{\ifx#3#4}
}
Won't the following work? (Yes, I need an answer.)
\def\\{#1}
\ifx\empty\\
% null
\else
% not null
\fi
where \empty is already \defn'd to be {}.
--
---------------------------------------------------------------------------
Navindra S Gambhir gam...@cs.cornell.edu
I would rather write programs to help me write programs than write programs
Yes, this works, and it is much nicer than the other two solutions
presented (and because of simplicity it may be considered nicer than my
solution). But it doesn't work in all circumstances. It is not totally
expandable, so it is "fragile" in LaTeX lingo; try
\write-1{\isthisnull{}}
where \isthisnull is defined as above.
It will not work if #1 contains any \outer tokens:
\isthisnull{\theorem....}
Also, a definition like
\long\def\isthisnull#1{\def\nullargument{#1}\ifx\nullargument\empty}
would mean that \isthisnull cannot be skipped over in conditional
text (it gives an extra \fi). This can be overcome with the definition
\long\def\isthisnull#1#2#3{\def\nullargument{#1}\ifx\nullargument\empty
#2\else#3\fi}
but that means both #2 and #3 must be read in and stored, so they can't
be overly long.
Donald Arseneau as...@reg.triumf.ca