42

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?

3
  • 4
    For comments. And used as \let\iffoo\iffalse.
    – Leo Liu
    Mar 1, 2012 at 15:37
  • For (semi)infix notation of conditionals: tex.stackexchange.com/a/43373 Mar 1, 2012 at 22:08
  • It's great to comment out entire sections or beamer slides during editing.
    – Tom Kelly
    Jul 10, 2018 at 8:26

6 Answers 6

56

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".)

1
  • 6
    +1 for the great answer, and the delightful extract from the man.
    – Zenon
    Mar 1, 2012 at 22:53
22

The following definitions are accepted by TeX:

\def\obrace{\iftrue{\else}\fi}
\def\cbrace{\iffalse{\else}\fi}

and, in expansion contexts, they can be used to make TeX see an opening and a closing argument delimiter; I've used this idea to build continuous fractions using a syntax \contfrac{1;2,3,4} that expands to

1+\cfrac{1}{2+\cfrac{1}{3+\cfrac{1}{4}}}

without the need for the user to ensure brace matching.

The main usage is, however, in the definition of new conditionals. When one says

\let\xyz\ify

(where \ify is any of the primitive conditionals), the control sequence \xyz is marked as being a conditional and so it participates in the nested conditionals matching. A command such as

\newif\ifxyz

actually defines the two commands \xyztrue and \xyzfalse, executing the latter. The two commands are defined as

\def\xyztrue{\let\ifxyz\iftrue}
\def\xyzfalse{\let\ifxyz\iffalse}

so that \ifxyz is always one of \iftrue or \iffalse and so can be nested in other conditional constructions.

Other programming styles don't use them; for example conditionals in LaTeX3 are of the form

\prefix_if_something:nTF {<condition>} {<true>} {<false>}

so that there's no explicit conditional nesting (but there's argument nesting).

Note that also \let\myelse\else and \let\myfi\fi mark the two control sequences \myelse and \myfi as being part of nesting conditionals matching; indeed both Plain TeX and LaTeX say \let\repeat\fi in order to be able to skip over \loop construction that happen to be in the "False" branch of another conditional.

2
  • Did you consider adding your continuous fraction code to the brace tricks question? Mar 1, 2012 at 22:06
  • @PhilippeGoutet Done.
    – egreg
    Mar 1, 2012 at 22:23
11

Every \ifXXX command that is not TeX primitive is always made equal to \iftrue or \iffalse. LaTeX contains plenty of these such as \if@twocolumn etc.

You usually don't use \iffalse itself, but some of these commands.

When you want to change \ifXXX to be true, you say \let\ifXXX\iftrue, which is exactly what \XXXtrue does. And similarly for false or course.

9

It can be useful to make a complex macro do different things based on a conditional switch. In the following, the conditional is not so complex.

\documentclass{article}

\newcommand*\ConditionallyDo[1]{%
   #1Yes, I did it.\else No, I didn't.\fi%
}

\begin{document}
    \ConditionallyDo{\iffalse} % -> No I didn't.
    \ConditionallyDo{\iftrue}  % -> Yes I did it.
\end{document}
1
  • Of course, such a command cannot be inside a conditional. Probably, saying \csname if#1\endcsname and calling \ConditionallyDo{true} or \ConditionallyDo{false} would be better.
    – egreg
    Mar 1, 2012 at 18:18
8

I guess you know it but …

\iffalse is used for Meta-Comments in .dtx files.

% \iffalse meta-comment
% 
% Copyright (C) 2012 by Tobias Weh
%                       www.tobias-weh.de
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{twfonts.dtx}[2012/03/01 v1.0 Private XeTeX package]
%</driver>
%<class>\NeedsTeXFormat{LaTeX2e}[2009/01/01]
%<class>\ProvidesPackage{twfonts}
%<class>   [2012/03/01 v1.0 Private XeTeX package]
%
%<*driver>
\documentclass{ltxdoc}

\begin{document}
  \DocInput{twfonts.dtx}
\end{document}
%</driver>
% \fi
%
% Documentation-Text
%
% \section{Implementation}
% A simple macro:
%    \begin{macrocode}
\newcommand{\macro}{Hello World!}
%    \end{macrocode}
%
\endinput

.dtx is a file for literate programming. The two \iffalse macros hide the code from TeX and keep it visible for the user (i.e. reader of the source code).

8

the usual answer for this kind of question is to see Victor Eijkhout's book, TeX by Topic (texdoc texbytopic works on many systems). it applies here too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .