# What are vertical and horizontal modes?

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.)
-
Please feel free to retag as appropriate. –  Willie Wong Mar 10 '11 at 19:22
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 Mar 10 '11 at 19:25
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. –  Gonzalo Medina Mar 10 '11 at 19:26
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 :-). –  Willie Wong Mar 11 '11 at 15:19

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.

-
\leavevmode is an almost identical Plain macro (no \protected) –  morbusg Mar 10 '11 at 22:16
@morsburg I know, LaTeX, is just syntactic sugar for plain. –  Yiannis Lazarides Mar 10 '11 at 22:35
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 Mar 11 '11 at 7:25
@morsburg I modified the answer to read Plain, LaTeX etc... –  Yiannis Lazarides Mar 11 '11 at 11:13
It's still morbusg. –  Hendrik Vogt Mar 11 '11 at 16:59

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.

-
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. –  Christian Lindig Mar 10 '11 at 21:24
@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.) –  Martin Scharrer Mar 11 '11 at 13:57
@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) –  Taco Hoekwater Mar 12 '11 at 7:39