50

Yiannis's answer to a question introduced me to the notion of "vertical mode". Doing a search on the internet turns up relatively little about the different modes of operation; all I've managed to find is that there are 6 modes, 2 each for horizontal and vertical, and the math mode and displayed math mode.

Unfortunately I don't own a copy of the TeX Book, nor do I have ready access to a copy at the moment. My questions are:

  1. What are the 6 different modes?
  2. For a user (as opposed to a package developer) of TeX, what are some of the practical things I need to worry about with regards to then? (The question linked above being an example of when the underbelly is exposed.)
4
  • Please feel free to retag as appropriate. Commented Mar 10, 2011 at 19:22
  • 4
    Someone with better understanding than I have will probably give an answer soon, but meanwhile you can have a look at chapter 6 of TeX by Topic (texdoc texbytopic).
    – Caramdir
    Commented Mar 10, 2011 at 19:25
  • 4
    Chapter 6 of TeX by Topic (eijkhout.net/tbt) describes vertical and horizontal modes. In Chapter 22 you'll find a description of math modes. Commented Mar 10, 2011 at 19:26
  • 1
    Even though neither answers addressed my second answer particularly, the TeX by Topic book mentioned by many responders addresses it handsomely. As such, Yiannis's answer won by a coin toss :-). Commented Mar 11, 2011 at 15:19

2 Answers 2

43

In order to understand the notion of modes, we first need to understand that the way TeX works is that it just typesets boxes. What is in these boxes is immaterial for TeX. If your manuscript contains only text, these boxes are built primarily while TeX is one of two modes. In the horizontal mode, it stacks boxes next to each other and in vertical mode boxes are stacked on top of each other. These are called horizontal or vertical lists.

So generally when TeX is in horizontal mode it is busy building lines of TeX and when it is in vertical mode it is stacking boxes of lines or paragraphs one on top of each other.

In one of the math modes it is reading a formula. Think of modes as program switches. Depending on what TeX is typesetting, the switch enables different modes to perform different tasks such as allowing or disallowing primitive commands and the like.

When TeX is in vertical mode or internal vertical mode, the first token of a new paragraph changes the mode to horizontal for the duration of a paragraph. You can also tell TeX explicitly to go into horizontal mode, instead of relying on such implicit mode-switching, by saying \indent or \noindent. Plain, LaTeX and pdfTeX have built-in macros to make easier to switch from one mode to another such as \leavevmode, which is simply a void box that is opened:

\protected\def\leavevmode{\unhbox \voidb@x}

The minutiae can be found in the TeXbook or TeX by Topic. Also the e-tex manual has a lot of macros that enable detection of modes. There is also a very good article in TUGboat about how the TeX processor works.

7
  • 1
    \leavevmode is an almost identical Plain macro (no \protected)
    – morbusg
    Commented Mar 10, 2011 at 22:16
  • 1
    Well, no, did you mean Plain (format) when you wrote that the macro is found in pdfTeX (engine)? Now the reader gets the impression that \leavevmode is only found in LaTeX (format) and pdfTeX (engine), and nowhere else (which is not true).
    – morbusg
    Commented Mar 11, 2011 at 7:25
  • 1
    It's still morbusg. Commented Mar 11, 2011 at 16:59
  • 1
    @YiannisLazarides You used the word 'stack', that boxes are stacked one on top of each other. Does it refer to the ADT stack, so a line is a stack of boxes-characters and a page is a stack of paragraphs? And when TeX stacks lines within a paragraph, is it a vertical mode? Or vertical mode is only when sticking paragraphs?
    – Vladimir
    Commented Oct 7, 2021 at 21:00
  • 1
    @Vladimir No actually they are linked lists. I used the word "stack", so one could visualize it.
    – yannisl
    Commented Oct 8, 2021 at 0:42
33

The TeXBook contains a full chapter (13) for the modes. The six modes are:

  • Vertical mode. Building the main vertical list, from which the pages of output are derived.
  • Internal vertical mode. Building a vertical list for a vbox.
  • Horizontal mode. Building a horizontal list for a paragraph.
  • Restricted horizontal mode. Building a horizontal list for an hbox.
  • Math mode. Building a mathematical formula to be placed in a horizontal list.
  • Display math mode. Building a mathematical formula to be placed on a line by itself, temporarily interrupting the current paragraph.

The difference for the normal and restricted horizontal mode is that in the restricted mode there can't be a line break. In short: when TeX builds up a line it is in horizontal mode. A \hbox or \mbox however is restricted horizontal mode.

The horizontal boxes are then stacked together in vertical boxes. I assume that the internal vertical mode can't include a page break.

3
  • 4
    Could you elaborate whether TeX can switch freely between these modes or whether modes must nest properly or some other restriction exists? Proper nesting would imply some recursive scheme and TeX would return to build a horizontal box after it is done building a vertical box to add it to the horizontal box. Commented Mar 10, 2011 at 21:24
  • 1
    @Christan: AFAIK TeX can switch "freely" and boxes can be nested similar like groups. There are however some commands which can't be used in certain modes. TeX will return to build the vertical box e.g. of the current paragraph after it is done with the horizontal boxes of the lines. (You have it backward in your comment.) Commented Mar 11, 2011 at 13:57
  • 1
    @Christian: Most mode transitions are possible,. but not all. For example, you cannot go from math into display math, or from a horizontal to vertical without first starting a vbox (there is no way to 'reenter' either un-restricted horizontal or vertical mode from within another mode) Commented Mar 12, 2011 at 7:39

You must log in to answer this question.

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