Does \leavevmode leave any tokens in the input stream

When the standard \leavemode is executed in vertical mode, the \unhbox primitive switches TeX to horizontal mode. The \unhbox token remains in the input stream at that point. This can be seen if we arrange to grab the first token in a paragraph using \everypar:

\def\temp#1{\def\temp{#1}\show\temp}
\everypar{\temp}
\leavevmode A
\bye

which yields

> \temp=macro:
->\unhbox .

This could potentially be an issue if some kind of look-ahead is in use. Of course, the unboxed empty box has no 'residual' effect on the vertical list being constructed.


The standard \leavevmode is the best definition with TeX90. However, when e-TeX is available, it is possible to use

\protected\def\leavevmode{\ifvmode\expandafter\indent\fi}

With this indention, TeX executes \indent before \everypar is inserted. Thus there is nothing 'stray' in the input. (Try the demo above to see this.)

The reason this needs \protected is the behaviour of TeX at the start of an \halign. There, TeX will expand normal macros but (probably surprisingly) \ifvmode is true. Thus the result would be 'wrong':

\halign{#\cr \leavevmode a\cr a\cr}
\def\leavevmode{\ifvmode\expandafter\indent\fi}
\halign{#\cr \leavevmode a\cr a\cr}
\protected\def\leavevmode{\ifvmode\expandafter\indent\fi}
\halign{#\cr \leavevmode a\cr a\cr}
\bye

The e-TeX \protected mechanism avoids this, making the \indent-based \leavevmode approach viable.


pdfTeX introduced \quitvmode as a primitive, which has a similar effect to the e-TeX definition given above. However, it's not quite the same as the e-TeX version: try for example

\protected\def\leavevmode{\ifvmode\expandafter\indent\fi}
$x_\leavevmode a$
$x_\quitvmode b$
\bye

I’m afraid I don’t fully understand this “question-with-answer”, or better, what its author was aiming to when he posted it. I had initally thought of explaining my doubts in a comment, but I found it difficult/impossible to remain within the 600-character limit, so I opted for an additional answer; and I believe that, after all, these remarks could indeed, in some loose sense, be classed as such.

First of all, I don’t deem it pointless to remark that the interaction between a ⟨horizontal command⟩ and the tokens stored in the \everypar register is explicitly and clearly documented in Chapter 24 of The TeXbook, more precisely on p. 283, where the effects, in vertical mode, of such a command are detailed:

[…] If any of these tokens occurs as a command in vertical mode or internal vertical mode, TeX automatically performs an \indent command as explained above. This leads into horizontal mode with the \everypar tokens in the input, after which TeX will see the ⟨horizontal command⟩ again.

The behavior of \leavevmode can easily be inferred from the above description—albeit, admittedly, only if one knows its definition.

In the second place, I’m not sure to understand where Joseph Wright means to get when he compares the behavior of pdfTeX’s \quitvmode and of his redefinition of \leavevmode in places where a ⟨math field⟩ is expected: it is obvious that the expandable, redefined \leavevmode vanishes without errors, while \quitvmode, being unexpandable and not qualifying as the beginning of a ⟨math field⟩, causes an error; but I can hardly regard the former behavior as a feature, and the latter as a bug: who would ever want to ensure that vertical mode has been left at the start of a subscript? I understand that \leavevmode is mainly used in macros and that, therefore, tends to show up in unforeseeable places; but wouldn’t it be much more sensible to emit an error message, should such an incongruous command appear in that position? Finally, note that Knuth’s \leavevmode would cause an error too, exactly as \quitvmode does.

Third, I think that a remark is in order as to why it is best to use e-TeX extensions, in particular a \protected definition, for the purpose of defining an \ifvmode-based, \halign-safe version of \leavevmode: one could wonder—as I did!—whether the good old trick of prepending \relax to the conditional couldn’t suffice for this. Indeed, TeX enters restricted horizontal mode as soon as it has checked (after expansion) that the first unexpandable token that follows a \cr (including the \cr that ends the preamble) is neither \noalign nor \omit, and it starts reading the “u-part” of the template for the first column only afterward (cf. section 768 of “tex-the-program”). The answer, in this case, is that this would yield a stray \relax when used in horizontal mode, breaking, for example, the “ff” ligature in f\leavevmode f. Once again, one could ask whether this actually proves important in any real-world scenario, but this time the argument that a \leavevmode might unpredictably wind up in such a position as part of a larger macro is not so easily dismissed. (Cf. the rationale behind the definition of, e.g., the \TextOrMath command. Note, by the way, that a ⟨math field⟩ begins with a ⟨filler⟩, so the \relax would not cause an error, nor affect the output, in a construct like $x_\leavevmode a$). Of course, Knuth’s definition suffers from the same problem, since \unhbox breaks ligatures as well.

Finally, here is some code that illustrates the claims set forth above:

% !TEX TS-program = pdftex

\def\test{
    {\tt \meaning\leavevmode}

    % Abc $x_\leavevmode a$ xxx;

    Note: ff vs. f\leavevmode f

    \medskip\halign{##\cr \leavevmode a\cr a\cr}\bigskip

    \hrule\bigskip
}

%%%%%%%%

% Original definition by Knuth:
\test

\def\leavevmode{\ifvmode\expandafter\indent\fi}
\test

\def\leavevmode{\relax\ifvmode\expandafter\indent\fi}
\test

\protected\def\leavevmode{\ifvmode\expandafter\indent\fi}
\test

% abc $x_\quitvmode b$ yyy.

\bye

Compile with plain TeX, of course, using pdfTeX as the typesetting engine. The code lines for the two “subscript” tests are initially commented out, to avoid errors during compilation: activate them at your leisure.

Tags:

Tex Core