[Tex/LaTex] an \if

conditionalstex-core

I'm reading TeX by Topic, but in little chunks (rather than from first page to last) and just read the stuff on conditionals. I want to be sure that I understand what's going on when TeX starts skipping stuff. What I've gleaned so far is that in, say, \iffalse ... \fi then:

  1. TeX doesn't do expansions.

  2. TeX keeps track of \if...s to ensure that the \fi really matches the \iffalse.

It's that last bit that I'm wanting to make sure that I get straight in my head. To make it a precise question:

What exactly is an \if...?

One reason for my worry is that I have some pseudo-ifs in my code, where a command starts \if... but isn't actually an \if. On the other hand, what if I had a command that didn't start with the three characters \if but happened to be \let to an \if? So, for example, which of the following count as an \if when the macro (not the definition of it) is used between \iffalse ... \fi:

  1. \let\ifabc=\ifx (so, to be clear, what happens in \iffalse ... \ifabc ... \fi, does the \ifabc count as an \if?
  2. \def\ifabc{\ifx}
  3. \edef\ifabc{\ifx}
  4. \let\ifabc=\SomePreviouslyDefinedCommandThatIsntAnIf
  5. \let\abc=\ifx

Best Answer

When TeX is skipping tokens in a false path of a conditional it looks at every token without expanding it and modifies an if-counter very similar to the brace counter. If the token is a primitive \if... token (e.g. \let to any of TeX's original conditionals like \if, \ifx, \ifnum, ...) the counter is increased and if it's identical to \fi (i.e. \fi or any macro let to it) the counter is decreased. The name of the macro is not important. Conditionals inside other macros are never seen.

To answer your questions in detail:

  1. \let\ifabc=\ifx (so, to be clear, what happens in \iffalse ... \ifabc ... \fi, does the \ifabc count as an \if?

    A: Yes, \ifabc counts as an if.

  2. \def\ifabc{\ifx}

    A: No, \ifabc isn't a if. It will work in the true path, because it is expanded there, but not in the false path.

  3. \edef\ifabc{\ifx}

    A: This wont work, because \ifx is expanded and therefore awaits its two input tokens and the final \fi. Even so, primitives can't be expanded to their underlying meaning, otherwise they wouldn't be primitives.

  4. \let\ifabc=\SomePreviouslyDefinedCommandThatIsntAnIf

    A: As I've said above , the name doesn't matter; this is not an if.

  5. \let\abc=\ifx

    A: The name doesn't matter; this is an if.

For further information about the if-switches see The TeXBook in "Chapter 20: Definitions (also called Macros)", page 207 ff.