[OS X TeX] ifemptyelse

Piet van Oostrum piet at cs.uu.nl
Sun Dec 19 11:07:13 EST 2004

>>>>> Peder Axensten <peder at axensten.se> (PA) wrote:

PA> I often need to check if some command is empty or not, so with the use of
PA> ifthen.sty I constructed
PA> \newcommand{\ifemptyelse}[3]{%
PA> 	\savebox{\tempbox}{\parbox{50cm}{#1}}%
PA> 	\ifthenelse{\lengthtest{\wd\tempbox < 1pt}}{#2}{#3}%
PA> }%

PA> I works pretty well, except when the tested argument is a counter (or
PA> something else) that is changed.

PA> For example
PA>      \newcommand{\test}{One\footnote{Two}}
PA>      \ifemptyelse{\test}{Is empty!}{Is '\test'!}
PA> will result in "Is One2!" (should be "Is 'On1'!", of course) and one
PA> footnote.

PA> Could anyone suggest definition of \ifemptyelse that works as expected?

It depends on what you mean with 'empty'. An empty argument? Or something
that generates no text? From your example above I guess it is 'no text'

For empty arguments:

\ifthenelse{\equal{#1}{}}{#2}{#3} should work, I think.

Otherwise, you have a problem. To see if the text is empty, it has to be
typeset, which can cause all kinds of side effects. Local side effects can
be undone by doing everything within a group {} or \begingroup\endgroup.
But global side effects cannot be undone unless you know which ones you can
expect. For example if it is only counters that can cause problems you can
temporarily redefine \setcounter \addcounter etc. within the group.
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl
--------------------- Info ---------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
           & FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Post: <mailto:MacOSX-TeX at email.esm.psu.edu>

More information about the MacOSX-TeX mailing list