TeX

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"&mdash;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
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
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

 * Badness : A measure of the quality of the spacing for a typeset line of text.
 * baseline:
 * hbox :
 * 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
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:
 * baseline : This is a horizontal line that vertically divides the box.
 * height : The distance from the baseline to the top of the box.
 * depth : The distance from the baseline to the bottom of the box.
 * width : 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
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
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.
 * Vertical mode
 * Internal vertical mode
 * Horizontal mode
 * Restricted horizontal mode
 * Math mode
 * Display math mode

TeX Keywords
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
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, \pretolerance= gives the syntax of one of TeX's integer parameters. In the source file, the 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  [\else ] \fi which gives the syntax of one of TeX's if-then-else control structures. The brackets here indicate that '\else ' need not be included in actual use. In either case, the brackets should not be used in actual use.

Sources on the Web

 * www.tug.org &mdash; TeX Users Group
 * ctan.tug.org/tex-archive/info/gentle/gentle.pdf &mdash; Michael Doob's guide to TeX
 * TeX for the Impatient
 * TeX Primitive Control Sequences &mdash; TeX Reference Manual
 * TeX by Topic &mdash; Book by Victor Eijkhout that has been made freely available.

Other wikibooks

 * LaTeX

TeX