TeX Core – What is \iffalse Used For?

conditionalstex-core

So this is something I've wondered about for a while. What use is a conditional that always evaluates to false? I guess it's a neat way to "hide code from LaTeX" but apart from that, is it ever useful?

Best Answer

Knuth's own answer to this is quite instructive: on p.211 of the TeXbook, he describes the evolution of the \phantom command. At first, he used macros \yes and \no to decide whether or not vertical or horizontal dimensions were requested:

\def\yes{\if00}
\def\no{\if01}

Clearly, these have the same effect (in principle) as \iftrue and \iffalse. However, he then learned that the way he defined \if... in his own programming language made this method infeasible:

\if...(\yes...\fi)...\else...\fi

I have inserted parentheses to show intent. If the \if... returns "false", then the entire first branch is skipped and not expanded; alas, TeX does not know what I indicated with those parentheses, so it terminates the \if with the first \fi, because \yes is not an "\if" as far as it is concerned and it does not use it to match nestings.

Thus, \iftrue and \iffalse are syntactic hooks to support abstract decision-making. Through them, the entire \newif mechanism (i.e. \newif\ifsomethingstrange, followed by \somethingstrangetrue when you decide that something strange has happened) is possible. In fact, \somethingstrangetrue just defines \ifsomethingstrange to be \iftrue; it uses \let, so TeX does treat this "\if" as a legitimate conditional.

The tradition of tautologic in programming has a bit of a history: there are true and false commands in Unix as well, because sometimes you have to indicate success or failure in a particular way. In that case, the OS expects a program to exit with a particular "return value", which these programs provide and nothing else. So if you want to do some kind of complex test and then branch off the result using this mechanism, you signal to the mechanism using one of these commands how you considered the test to have concluded.

(The man pages for these commands are hilarious: true "does nothing, successfully", but poor false "does nothing, unsuccessfully".)