TeX is a language used to typeset documents, especially ones containing mathematics. It has both programming and mark-up features. In this book we will describe "primitive TeX"—the basic engine for the macro languages plain TeX, LaTeX, ConTeXt, and so on. If you are new to TeX, then this is probably not what you want to read: you may be looking for LaTeX, a macro package for TeX that makes it easier to produce standard documents.


TeX is Donald Knuth's macro language, written to typeset documents. TeX can in principle be used for any kind of program (it is a Turing complete programming language).

Other sources edit

The canonical source for learning the TeX language, both to write documents and to program, is the TeXbook by Donald Knuth. There's also a comprehensive programmer's reference called TeX by topic which the author has generously made available free at his website as well as TeX for the Impatient.

The Structure of TeX edit

We describe the basic anatomy of TeX: the registers, the parameters, and other resource that affect the processing of a TeX source file.

Basic Definitions edit

A measure of the quality of the spacing for a typeset line of text.
overfull error
TeX couldn't produce a result that doesn't overfill a box, and has given up. There is usually a hyphenation or justification problem: moving the last word on the line to the next line would make the spaces in the line wider than the current limit; keeping the word on the line would make the spaces smaller than the current limit, so the word is left on the line, but with the minimum allowed space between words, and which makes the line go over the edge.
underfull error
TeX couldn't produce a result that appropriately fills a box, and has given up. TeX cannot stretch the line wide enough to fit, without making the spacing bigger than its currently permitted maximum. The badness (0-10,000) indicates how severe this is.

Boxes edit

The concept of a box is fundamental to TeX. Boxes represent the content that make up a typeset page. For example, letters, lines of text, paragraphs, mathematical formula and its composite symbols are all conceptually just boxes that TeX pastes together to build the page, which itself is also a box. Boxes are often composed of smaller boxes.

Each box has a number of properties that TeX uses to paste the boxes together using various algorithms. These are:

This is a horizontal line that vertically divides the box.
The distance from the baseline to the top of the box.
The distance from the baseline to the bottom of the box.
The horizontal length of the box.
reference point
The point where the baseline joins the left side of the box.

As an example, the letter 'a' is placed in a box so that is sits directly on the baseline; while letters such as 'p', 'g', or 'j' extend below the baseline. When TeX is building a line of ordinary text, all the baselines are aligned, and so a line of characters is positioned as one would expect. Font designers determine how to place each glyph within its respective box.

Glue edit

While boxes represent the contents of a page, glue represents the white space that fits in between boxes, and as TeX is building the page this glue can expand or contract to allow the contents to be positioned optimally. This glue is eventually 'set' after which it can no longer adjust--it's just fixed white space.

Many types of predefined glues exist to meet the different needs that can arise with typesetting.

Modes edit

At each point in the processing of a source file, TeX is in one of the following modes:

How TeX interprets the source file depends on the current mode.

Macros edit

Fonts edit

Math Fonts edit

Hooks edit

TeX Algorithms edit

Tokenization edit

Expanding Macros edit

Setting the Glue edit

Line Construction edit

Page Construction edit

The Construction of Mathematical Formulas edit

The Construction of Tables edit

Hyphenation edit

Output edit

TeX Keywords edit

TeX has 25 keywords. These words have no backslash before them, and they are not case-sensitive. TeX interprets each of these words as a keyword only in certain contexts.

TeX Primitives edit

Here we describe each of the 325 TeX primitive control sequences. Each entry gives a synopsis, description, and examples. The synopsis gives the proper syntactical context of the control sequence. For example,


gives the syntax of one of TeX's integer parameters. In the source file, the <integer> needs to be replaced by an actual integer as described in the 'description' section of the entry \pretolerance. In general anything of the form <...>, where the ellipse is some descriptive word, needs to be replaced by something appropriate; the 'description' section will tell you what is allowable.

Sometimes part of the syntax is optional, this is indicated by placing it in between brackets, as in

\ifmmode <TeX code 1> [\else <TeX code 2>] \fi

which gives the syntax of one of TeX's if-then-else control structures. The brackets here indicate that '\else <TeX code 2>' need not be included in actual use. In either case, the brackets should not be used in actual use.

Sources on the Web edit

Other wikibooks edit