TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am more and more interested in the system TeX (and LaTeX) and I want to study it more deeply. I am not telling that I want to learn how to use it, but I want to understand its mechanism now.

The first thing that I can imagine is to download its source code Plain Tex. But

...the current TeX software is written in WEB, a mixture of documentation written in TeX and a Pascal subset in order to ensure portability... (Wikipedia)

I am an amateur C programmer with a little experience of Win32 API, and I always use Microsoft Visual Studio to do programming.

I have totally no idea what to do next with the souce code above, for example, compile it and get outputs (on Windows and on Linux), use the outputs to compile a .tex file and get the output.

Can some expert(s) talk and explain a little about this beautiful sytem, or, at least, tutorials about how to build it, please?

share|improve this question
Could you explain why you need to compile the code, rather than say read it in typeset form and use plain TeX pre-compiled? – Joseph Wright Apr 29 '13 at 12:10
@Joseph: Because successful compiliation of it make me feel good and confident :) – user565739 Apr 29 '13 at 12:12
@JosephWright: Yes, I know these are 2 different things. But I am aware that I want to know more about TeX rather to know how to use it (for now). – user565739 Apr 29 '13 at 12:32
@Kaz: I don't understand your point. I am not interested in Firefox, so I don't want to compile it. – user565739 Apr 29 '13 at 19:39
Like you, I also wanted to study the TeX code. After all, it was written by an eminent computer scientist, and, for a large piece of software, it is remarkably free of bugs. It looked like something significant could be learned. But, I gave up after a while. Mostly because the code is full of clever tricks to pack data into memory locations to save space. This made sense 30 years ago, but not so much today, and it makes the code very difficult to read (for me, anyway). There is a system called JavaTex, by Tim Murphy, which might be a bit easier to read, maybe. But I ran out of time. – bubba Apr 30 '13 at 10:31
up vote 19 down vote accepted

You are linking there to plain.tex which is a file written in TeX not the source of tex-the-program (which is tex.web)

These days if you want to compile from source it is probably best to start with a full download of the texlive build sources.

The sources are at


and that page has hints about where to start if you want to compile. See in particular:


Good luck:-)

share|improve this answer
Thank you, David. But I don't like to build a whole system like TexLive. It is far from what I want to know. My goal is to know the TeX core instead. But thanks again. – user565739 Apr 29 '13 at 12:27
@user565739 Building just tex without a minimal support stuff for fonts and so one will not give you a workable system. Perhaps take a look at KerTeX if you want something smaller that you can compile. – Joseph Wright Apr 29 '13 at 12:32
@user565739 well.. the original tex sources are tex.web which is pascal more or less, but you are hard pushed to find a usable pascal compiler these days so you probably want at least web2c if not the whole of texlive. web2c as its name suggests translates web to c so it can be compiled with C but you also need the kpathsea file search library if you want a tex that can handle modern filesystems, and so it goes. It is probably easier to start with texlive and cut things out of the makefiles than to just get the original Knuth sources and try to build on a current system. – David Carlisle Apr 29 '13 at 12:33
@DavidCarlisle: So now I am curious that how texlive do with tex.web? When I build texlive, it also compile tex.web? – user565739 Apr 29 '13 at 12:37
@user565739 yes of course the heart of the sources is still tex.web but you need to build the scaffolding before you build the building and web2c provides that. texlive itself of course then goes on to provide all the tex packages and updates mechanisms and stuff which you don't need but still it's easier to download a working build setup and run the build scripts and watch and learn what happens rather than just download the raw web sources and a pascal compiler and then try to recreate a working system as it was in 1982. – David Carlisle Apr 29 '13 at 12:42

David Carlisle explains how to compile the sources for the modern versions of Tex that are the basis for Texlive (Pdftex, Xetex, and Luatex, among others). These derive from Karl Berry's Web2c fork of Knuth's source code, which is a mechanical way of translating code Web sources to C code that can be compiled just about anywhere.

If you want to compile sources that are closer to what Knuth wrote (and documents in The TeXbook), take a look at:


This project allows you to compile Pascal WEB sources directly, using GNU Pascal. This apparently wasn't trivial; as the author, Wolfgang Helbig, writes:

I was somewhat intrigued while building TeX from its sources, since some of these depend on others to be built and installed. Knuth wrote these programs in the WEB language (WEB is only remotely related to the last W from CERN's WWW). WEB programs are converted to Pascal sources by tangle and to a TeX input file by weave. Of course, tangle and weave are WEB programs as well. So one needs tangle to build tangle---and weave and TeX to read a beautifully typeset WEB program. But don't despair, I cut this indefinite recursion and provided tangle.p, the Pascal source of tangle, and tex.pdf. It shows what, why and how I changed Knuth's program.

His tex.pdf documents in minute detail these changes.

share|improve this answer

All the programs in the TeX series (TeX by itself, but also METAFONT and all the auxiliaries) are written in WEB, but to ease the translation, D. E. Knuth has not really written in a flavour of Pascal, but in some very abstract Pascal; much an Algol: there is not a lot of Pascal idiosyncrazies left specifically to allow to convert the programs to whatever. The simplest, nowadays, is to convert the Pascal like code in raw C.

In kerTeX this is what is made by kertex_M: the matrix, the tools needed to obtain C code. The matrix tools are compiled... on the matrix, to obtain C code that can be compiled for whatever target. (Allowing cross-compilation).

If you want to see the thing, just use kerTeX and with the get_mk_install.sh (or rc) program, when asked if you want the program to remove the intermediary products (including the *.c files in this case), just answer : NO to keep them.

For the bootstrapping problem (tangling tangle), D. E. Knuth has written that the first version was done by hand: he mimicked by hand what tangle would do (this is why such a program has to be basic, the same way a compiler has to be able to bootstrap a more basic version of itself to use itself to compile more complex one). This can also be done with text tools (I have sketched in kertex_M/bin1/tangle/tangleboot.sh such a program; it is not used, and lacks change file support).

The conversion from Pascal to C was needed because Pascal was not standardized, Pascal compilers not ubiquitous etc. And D.E. Knuth has also tried several years ago to encourage the possibility to compile TeX and al. with a Pascal compiler, just to be sure his programs will not be unavailable because of some compiling nightmare. And it was a pain.

It is more simple to convert to C, since it is, on purpose, a very basic Pascal (bibtex is far more hard to convert because it uses Pascal idiosyncrazies).

For information, the conversion from Web to C was web-to-c, initially from Tomas Rokicki. This is still what is used with TeXLive, or what has been used as a basis for kerTeX.

share|improve this answer
Welcome to TeX.sx! – Peter Jansson Apr 29 '13 at 15:10
No use of ctangle and its ilk then in TeXlive? Strange... – vonbrand Apr 29 '13 at 18:46

The original sources for TeX (and friends) can be found on Knuth's CTAN. All the sources have very comprehensive documentation, but trying to compile them is still an epic task.

TeX is written in WEB, a programming language invented by Knuth. So first we're going to need WEB.

WEB is written in WEB itself, it consists of two programs: weave which produces the TeX documentation from a WEB program, and tangle which produces Pascal code from a WEB program. To compile the WEB system we need an implementation of tangle; you can either get one from an existing TeX package, or you can compile it from Pascal source.

Note that if you want to read about the implementation of any WEB program you can use weave and TeX to produce copious documentation; this is a good starting point to Knuth's code. (For TeX and Metafont you can buy a printed version of the WEB output as Computers and Typesetting volumes B and D respectively.)

Now we need to talk a little about the dialect of Pascal the Knuth uses, which he calls Pascal H. TeX was written before Pascal was standardised; to my knowledge no native Pascal H compiler exists and it is not compatible with modern Pascal compilers. However Knuth wrote the programs in a relatively portable way so it's only moderately Herculean to port them. At this point you have some choices:

  1. Write a compiler for Knuth's Pascal H
  2. Port the WEB source to an existing Pascal dialect using change files
  3. Translate Knuth's Pascal H to another programming language

tex-gpc takes approach 2, TeX live (and Miktex) take approach 3 via web2c.

Now if you can do this the actual process of initialising and running TeX (initex, fonts, etc.) will be relatively easy; make sure you validate your build against TRIP (see the TeX sources). If you're feeling adventurous do the same for Metafont, the TeX tools, Metafont tools, and WEB.

About the design of TeX and Metafont: these programs were designed by Knuth to be highly robust, efficient and portable in the late 1970s. Today programmers take for granted the speed of modern processors and programming standards that allow them to write adequately functioning programs much more quickly. Much of what happens in these programs (e.g. carefully enumerating character codes, statically allocating memory at compile time, on-line error recovery) rarely happens in today's programming; and many of the modern annoyances (having to compile LaTeX twice for back references, difficulty with fonts, the intricacy of the macro language) are a result of these design goals and decisions. I wouldn't advocate Knuth's methods for most projects today involving multiple people, efficient computers, and tight deadlines. Still TeX is among the oldest programs to still be running today (and into the future unless LuaTeX supplants it), delicately designed, intricately implemented, pretty portable and copiously documented.

Good luck!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.