97

I am beginner of LaTeX. From many examples I found, I notice that it's very common to use command \leavevmode. I can't find any information about this command. Could anyone tell me what's the function of it and how to use it?

1

2 Answers 2

105

The \leavevmode is defined by LaTeX and plainTeX and ensures that the vertical mode is ended and horizontal mode is entered. In vertical mode, TeX stacks horizontal boxes vertically, whereas in horizontal mode, they are taken as part of the text line.

For example \mbox{..} is defined as \leavevmode\hbox{..} to ensure that horizontal mode is entered if it is used at the beginning of a paragraph. If you only use \hbox{ } it is stacked above the following paragraph instead.

Compare:

Text\par\hbox{Hello} World

Result:

  Text
Hello
  World

with:

Text\par\mbox{Hello} World

Result:

  Text
  Hello World

You see that in the first case the \hbox is stacked with the two paragraphs vertically (but without paragraph indention) because it is processed in vertical mode. In the second case horizontal mode is entered first and so Hello is processed as part of the second paragraph.

Use \leavevmode for all macros which could be used at the begin of the paragraph and add horizontal boxes by themselves (e.g. in form of text).


For further reading about \leavevmode please see "The TeXBook" by Donald E. Knuth, Appendix A, section 13.1, page 313 as well Appendix B, page 356.

5
  • Where can I read up about \leavevmode? Why don't I have to write \leavevmode in the beginning of a paragraph, but do have to write it within a macro that is in the beginning of a paragraph? Also, I don't see the problems you describe when I omit \leavevmode in tex.stackexchange.com/a/49913 - why not?
    – root
    Commented Jan 6, 2019 at 20:12
  • 2
    @root: \leavevmode is described in "The TeXBook" by Donald E. Knuth. It is defined as \unhbox\voidb@x. This unboxes an empty horizontal box, which is similar to \usebox but removes the outer box and inserts its content directly. It works because the TeX engine needs to switch to horizontal mode to unbox an horizontal box. Normal text starts horizontal mode automatically, that is how the TeX engine got programmed. You need to add \leavevmode manually on \hboxs as TeX does not know if this box is just part of the paragraph or a whole paragraph (which is a hbox as well internally) Commented Jan 7, 2019 at 7:12
  • 1
    @root: I added now a reference to The TeXBook to my answer. Commented Jan 7, 2019 at 7:19
  • \leavevmode is also used in ConTeXt, you need it you want to put something after a picture in the same line, or to verticaly align an image in a table. There are certainly other uses I am not aware of.
    – sztruks
    Commented Jan 7, 2019 at 7:39
  • To complete this answer, let me comment on how I use this in a particular example: if you use \begin{itemize} right after \begin{theorem} you will find that the first item is displayed right after the word Theorem XX, whereas the following one are listed at the beginning of the new line. However, if you add \leavevmode in between \begin{theorem} and \begin{itemize} you will find that all the items are vertically aligned, starting a line after the word Theorem is displayed. Commented Nov 25, 2020 at 10:39
12

With the 2018/12/01 release of LaTeX something has changed about \leavevmode.

The command didn't change, but a few others were modified. In particular

  • \thinspace
  • \negthinspace
  • \enspace
  • \finph@nt (used by \phantom, \hphantom and \vphantom)
  • \finsm@sh (used by \smash)
  • \big, \Big, \bigg, \Bigg

were modified to add \leavevmode@ifvmode, which is a sort of ”implicit” \leavevmode.

The problem with these commands was that if issued in vertical mode they wouldn't initiate a new paragraph. For instance, \thinspace, \negthinspace and \enspace would add vertical spacing and

... end of a paragraph.

\phantom{X}Other text

would produce an empty line, with “Other” at the normal indentation.

With the new release, all the above mentioned commands will properly start a new paragraph, if necessary (that is, if found after the end of a paragraph like in the previous example). The definition of \leavevmode@ifvmode is

% latex.ltx, line 1633:
\protected\def\leavevmode@ifvmode{\ifvmode\expandafter\indent\fi}

This makes some previous usages of \leavevmode redundant. Typical code where \leavevmode is no longer necessary is something like

\newcommand{\foo}[1]{\leavevmode\smash{#1}...}

Code that relied on the feature of \smash or \phantom to not initiate vertical mode would no longer work as expected, though. The cases are rare, as far as we know. This can be solved by adding a further level of boxing. So code such as

\newcommand{\foo}[1]{\vadjust{\smash{#1}}}

(that I saw a couple of days ago) should become

\newcommand{\foo}[1]{\vadjust{\hbox{\smash{#1}}}}

because \hbox still doesn't initiate horizontal mode.

2
  • When I recently updated one of my projects from texlive2015 to texlive2019 all my page makeup was destroyed because of this change. Is it save to use \let\leavevmode@ifvmode\relax in my project style to restore the previous layout? Also, is there a way to get automatically notified of that kind of changes n Latex's core?
    – Lupino
    Commented Oct 12, 2020 at 9:53
  • 2
    @Lupino Sorry for your inconvenience. Maybe with some more details it's possible to suggest a better workaround that avoids changes in the document environment. Making \leavevmode@ifvmode equal to \relax doesn't seem the best. Doing texdoc ltnews shows the news related to each release.
    – egreg
    Commented Oct 12, 2020 at 10:07

You must log in to answer this question.

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