# 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"—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.

#### 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:
* Vertical mode
* Internal vertical mode
* Horizontal mode
* Restricted horizontal mode
* Math mode
* Display math mode
How TeX interprets the source file depends on the current mode.

### Hooks
## TeX Algorithms
### Output
## 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.
| at by bp cc cm | dd depth em ex fil | fill filll height in minus | mm mu pc plus pt | sp spread to true width |

## 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,
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
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.
| \ \- \/ \above \abovedisplayshortskip \abovedisplayskip \abovewithdelims \accent \adjdemerits \advance \afterassignment \aftergroup \atop \atopwithdelims \badness \baselineskip \batchmode \begingroup \belowdisplayshortskip \belowdisplayskip \binoppenalty \botmark \box \boxmaxdepth \brokenpenalty \catcode \char \chardef \cleaders \closein \closeout \clubpenalty \copy \count \countdef \cr \crcr \csname \day \deadcycles \def \defaulthyphenchar \defaultskewchar \delcode \delimiter \delimiterfactor \delimitershortfall \dimen \dimendef \discretionary \displayindent \displaylimits \displaystyle \displaywidowpenalty | \displaywidth \divide \doublehyphendemerits \dp \dump \edef \else \emergencystretch \end \endcsname \endgroup \endinput \endlinechar \eqno \errhelp \errmessage \errorcontextlines \errorstopmode \escapechar \everycr \everydisplay \everyhbox \everyjob \everymath \everypar \everyvbox \exhyphenpenalty \expandafter \fam \fi \finalhyphendemerits \firstmark \floatingpenalty \font \fontdimen \fontname \futurelet \gdef \global \globaldefs \halign \hangafter \hangindent \hbadness \hbox \hfil \hfill \hfilneg \hfuzz \hoffset \holdinginserts \hrule \hsize \hskip \hss | \ht \hyphenation \hyphenchar \hyphenpenalty \if \ifcase \ifcat \ifdim \ifeof \iffalse \ifhbox \ifhmode \ifinner \ifmmode \ifnum \ifodd \iftrue \ifvbox \ifvmode \ifvoid \ifx \ignorespaces \immediate \indent \input \inputlineno \insert \insertpenalties \interlinepenalty \jobname \kern \language \lastbox \lastkern \lastpenalty \lastskip \lccode \leaders \left \lefthyphenmin \leftskip \leqno \let \limits \linepenalty \lineskip \lineskiplimit \long \looseness \lower \lowercase \mag \mark | \mathaccent \mathbin \mathchar \mathchardef \mathchoice \mathclose \mathcode \mathinner \mathop \mathopen \mathord \mathpunct \mathrel \mathsurround \maxdeadcycles \maxdepth \meaning \medmuskip \message \mkern \month \moveleft \moveright \mskip \multiply \muskip \muskipdef \newlinechar \noalign \noboundary \noexpand \noindent \nolimits \nonscript \nonstopmode \nulldelimiterspace \nullfont \number \omit \openin \openout \or \outer \output \outputpenalty \over \overfullrule \overline \overwithdelims \pagedepth \pagefilllstretch \pagefillstretch \pagefilstretch \pagegoal | \pageshrink \pagestretch \pagetotal \par \parfillskip \parindent \parshape \parskip \patterns \pausing \penalty \postdisplaypenalty \predisplaypenalty \predisplaysize \pretolerance \prevdepth \prevgraf \radical \raise \read \relax \relpenalty \right \righthyphenmin \rightskip \romannumeral \scriptfont \scriptscriptfont \scriptscriptstyle \scriptspace \scriptstyle \scrollmode \setbox \setlanguage \sfcode \shipout \show \showbox \showboxbreadth \showboxdepth \showlists \showthe \skewchar \skip \skipdef \spacefactor \spaceskip \span \special \splitbotmark \splitfirstmark \splitmaxdepth \splittopskip \string \tabskip \textfont | \textstyle \the \thickmuskip \thinmuskip \time \toks \toksdef \tolerance \topmark \topskip \tracingcommands \tracinglostchars \tracingmacros \tracingonline \tracingoutput \tracingpages \tracingparagraphs \tracingrestores \tracingstats \uccode \uchyph \underline \unhbox \unhcopy \unkern \unpenalty \unskip \unvbox \unvcopy \uppercase \vadjust \valign \vbadness \vbox \vcenter \vfil \vfill \vfilneg \vfuzz \voffset \vrule \vsize \vskip \vsplit \vss \vtop \wd \widowpenalty \write \xdef \xleaders \xspaceskip \year |

## Sources on the Web

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

## Other wikibooks

* LaTeX