Consider the following example:


    \ifnum1=#1% <- here a space is missing
        \expandafter \@firstoftwo
        \expandafter \@secondoftwo


With 1=1 it prints false, with 1=2 it prints truefalse.

I know that this is a problem of \ifnum expanding too much hoping to find a continuation of the number and that this can be fixed by inserting a space after #1.

However, I do not understand what exactly is happening.

This answer seems related but I am still unable to explain what is going on.

1 Answer 1



Never forget a space after a constant, unless you know precisely why you omit it (the \romannumeral trick is a prime example of why one might want to omit a space after a constant).

What happens?

What happens is that, due to the lack of a terminator for the number being evaluated, \expandafter is expanded; this touches \else, so, by rule, TeX inserts \relax (the “frozen” type) in front of \else (note that \expandafter has already gone). See Normal \relax vs. frozen \relax.

Thus you get the same as


Now expansion resumes, because the number is still unfinished and \@firstoftwo returns \relax; the test is then evaluated to true so TeX remains with


The \relax token is executed (or stored in the case of \edef) and \else is gobbled up and we get


Now \expandafter removes \fi and you end up with


so finally \relax false. If you do


the terminal will indeed show

> \test=macro:
->\relax false.

If you add \tracingmacros=1 before the call to \fail, the log file will show

\fail #1->\ifnum 1=#1\expandafter \@firstoftwo \else \expandafter \@secondoftwo

\@firstoftwo #1#2->#1

\@secondoftwo #1#2->#2

confirming my analysis.

With 《...》 I denote tokens that have already been sent to the further level.

If the call is \fail{2}{true}{false}, the first step yields


Since the number is not yet finished, \@firstoftwo is expanded, returning \relax:


The test now can be evaluated to false, so everything up to \fi (as \else no longer is present) is removed without expansion, leading to


and \fi now disappears. Indeed, \edef\test{\fail{2}{true}{false}}\show\test prints

> \test=macro:

on the terminal.

What about if you exchange \@firstoftwo and \@secondoftwo?


    \ifnum1=#1% <- here a space is missing
        \expandafter \@secondoftwo
        \expandafter \@firstoftwo



\tracingmacros=1 \tracingonline=1




Now the first level expansion will again add \relax, but we get


As before, \@secondoftwo is expanded, returning \else. Oh, but the number is yet unfinished, so a further frozen \relax is added, to yield


Now the conditional is evaluated to true, and the test is removed:


As before, \relax is sent down to the stomach and \else removes all tokens up to the matching \fi, without expansion:


The \fi disappears and we remain with the equivalent of

  • now I see. And in the other case, with 1=2, \@firstoftwo gobbles the \else in the same way so that TeX discards everything until \fi when reading the \relax and evaluating \ifnum. Therefore the two groups {true}{false} remain untouched and are printed to the pdf. Thank you.
    – jakun
    Jun 12, 2019 at 12:46
  • 1
    @jakun I added some further analysis and fixed a weakness in the description.
    – egreg
    Jun 12, 2019 at 12:51

You must log in to answer this question.

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