5

There was I, procrastinating over some TeX code while I should have been writing my dissertation. The code was working more or less fine (better than the writing, at least), until a mysterious \relax popped up from nowhere!

At first I though it was something stupid from my end, but when I reduced the code to a bare minimum I realised I had no idea what was going on. I reduced the code to use basically only primitives, so it shouldn't be any coding problem. Here's the guilty code:

\def\useIInnn#1#2#3{#2}
\def\useIInn#1#2{#2}
\detokenize\expandafter{\ifnum0=0\expandafter\useIInnn\fi\useIInn{1}{\BOOM}}
\bye

and its output is (surprisingly, for me):

\relax \fi {1}{\BOOM }

Where did that \relax come from?


I realise that the test wouldn't result in 0=0 because \ifnum would continue expanding tokens (as far as I understand, it would make 0=01 and result false; I corrected this in the code :). However the \relax remains a mystery for me.

6

Let's see what happens. The tokens \ifnum0=0 are not a full test, because by rule TeX expands tokens until either finding an unexpandable token that cannot be interpreted as a digit; if this token is a space, it will be swallowed.

The token following 0 is \expandafter, which expands the \fi: oh, this means that the conditional has to be evaluated! In such cases, that is, whenever a conditional text is unfinished at the time \else or \fi appear and have to be expanded, TeX inserts a special \relax token, called frozen \relax.

You get a frozen \relax also in cases such as \if x\fi; two in the case of \if\fi.

This is module 379 in tex.web.

  • Hm, cool, it's a type of safety. It's easy to see the \relaxes appearing with \unravel{\expandafter{\if\fi}} (I actually saw it earlier, but it didn't make sense). Thanks :-) – Phelype Oleinik May 11 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.