TeX: The Program

Background

To simply use TeX, you don’t need to know anything about how the TeX program itself is implemented.

If you do want to read the source code of the TeX program, you could just do it, as it has both been published in book form:

and is available as a PDF:

However, neither is perfect:

This webpage will attempt to provide the best of both worlds, and eventually to explain the program and make it easily understandable. But it is still under construction, and will take some time.

Book pages

In the meantime, here are some pages from the book that are not in the PDF (scans for now; hope to transcribe and/or create equivalents):

Preparatory reading

Additionally, to become familiar with the WEB style of writing programs, you may also want to read:

WEB itself

Smaller programs

Then, you may want to work your way up to TeX from smaller programs written by Knuth in the same style: I’ve prepared a List of WEB files. Specifically, a possible reading order, from smallest to largest, is:

Program Pages Sections Fraction of TeX
POOLTYPE 7 + 4 20 + 2 ≈ 1.4% to 2%
GLUE 8 + 3 26 + 1 ≈ 1.6% to 2%
DVITYPE 47 + 7 111 + 2 ≈ 8% to 10%
TANGLE 66 + 9 187 + 2 ≈ 13.5% to 14%
WEAVE 98 + 12 263 + 2 ≈ 19% to 20.5%
TEX 478 + 57 1378 + 2 100%

Of these,

I have separate pages for each of these programs on this site:

Totally unrelated to TeX, but you could look at other “literate programs” entirely: Knuth’s CWEB programs, or the (Academy Award winning!) Physically Based Rendering book (see random chapter).

Pascal

Note that these two programs are both compilers. TeX itself is written like a compiler, so you’ll find many similarities.

Other versions of TeX

Videos

Finally, after having read these smaller programs and having gained a bit of familiarity with Pascal, before reading the full TeX program I strongly recommend the series of 12 lectures that Knuth gave in 1982 called The Internal Details of TeX82. They are available on YouTube, but I’ve embedded them on this website, with some comments, here.

Also, as you read the program / watch the videos, you can also try solving these exercises from a course DEK gave about TeX:

History

It is unclear how much this would help, but often the earlier versions of a program are less complex, or at least illuminate how the program got into its current state.

The program

If you’re done with all the prerequisites and are ready for reading TeX itself, click here for a raw dump of TeX.

Random observations

A debugger may help. I started writing something (has a really bad interface and is implemented in a stupid way currently).

\expandafter\expandafter\expandafter\meaning\expandafter\uppercase\expandafter{a}
\end

After looking at the program.


That's the end of this page. If you have any feedback about anything on this site, you can contact me here. To go back to the top-level page (if you are not on it already), click here.