[Tex/LaTex] Best practice for getting “If not foo” conditionals

best practicesconditionals

So, here are two ways you might set things up such that \dostuff is done when foo is false.

The "new conditional" method

If you know when foo is going to be set, then after that do:

\newif\ifnotfoo
\iffoo\notfoofalse\else\notfootrue\fi

And then use the \ifnotfoo test.

\ifnotfoo\dostuff\fi

The "else" method

Whenever you need to \dostuff, just do:

\iffoo\else\dostuff\fi

Is either method recommended over the other? If so, why? (Or is there another method which is even better?

Best Answer

Hard to comment on efficiency (one of your tags) without knowing more context. \footrue and \foofalse are really each just a \let. So if the only reason that this token is let to one definition or another is so that you can decide whether or not to "do stuff" then you don't need the if token at all, then you don't need the test.

Just define

\def\foofalse{\let\dostuff\realstuff}
\def\footrue{\let\dostuff\relax}

then you can replace the \iffoo construct with simply

 \dostuff

and it will either do nothing or the real definition depending on whether \foofalse or \footrue was most recently executed. Since this saves expanding \iffoo and skipping over the conditional text it is more efficient in principle but whether the difference is measurable on modern machines I haven't checked. It is unlikely that any of these make any real difference unless you do (literally) millions of tests.

Related Question