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?

  • Unless you plan to do something very unusual, understanding the source of TeX is not necessary to use TeX. Certainly I would not claim to know much Pascal, but I'm quite good at programming TeX.
    – Joseph Wright
    Apr 29, 2013 at 12:29
  • 1
    One piece of advise from an old fart: Concentrate on what is (more or less) immediately useful. That way you have more motivation, and the risk of getting sidetracked is much less. TeX is not a good way to learn how to build packages, it uses a very idiosyncratic language (web), and was written to install on all sort of extremely weird machines/operating systems (most of them long defunct).
    – vonbrand
    Apr 29, 2013 at 18:51
  • 1
    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, 2013 at 10:31
  • 1
    Also, I agree with vonbrand -- Tex includes a lot of exotic code that was required to make things portable 30 years ago.
    – bubba
    Apr 30, 2013 at 10:34

5 Answers 5


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:-)

  • 2
    @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, 2013 at 12:32
  • 3
    @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. Apr 29, 2013 at 12:33
  • @JosephWright: (Maybe very stupid) After compile the source, I won't get the fonts?
    – user565739
    Apr 29, 2013 at 12:33
  • 1
    @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, 2013 at 12:37
  • 4
    @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. Apr 29, 2013 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.


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!


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.

  • No use of ctangle and its ilk then in TeXlive? Strange...
    – vonbrand
    Apr 29, 2013 at 18:46

Talk is cheap. Show me the code. ~Linus Torvolds

  1. Install Free Pascal Compiler (fpc) on the system.

    pacman -S --noconfirm fpc # Arch
    apt-get install fpc       # Debian
  2. Download, then bootstrap tangle and font-related programs.

     export TEX_HOME="$HOME/dev/tex"
     export PATH="${PATH}:${TEX_HOME}/work/bin"
     mkdir -p $TEX_HOME
     cd $TEX_HOME
     wget http://mirrors.ctan.org/systems/knuth/dist.zip
     wget http://mirrors.ctan.org/systems/knuth/local.zip
     wget http://mirrors.ctan.org/systems/unix/tex-fpc.zip
     for i in *.zip; do unzip $i; done
     rm *.zip
     mkdir work && cd work
     mkdir -p TeXinputs TeXformats TeXfonts PKfonts DVIPSconf
     mkdir -p MFbases MFinputs
     mkdir -p bin
     cd ..
     cp dist/lib/*.tex work/TeXinputs
     cp dist/cm/*mf dist/lib/*mf work/MFinputs
     cp local/cm/*mf local/lib/*mf work/MFinputs
     cp tex-fpc/shell/* work/bin
     fpc tex-fpc/tangle.p
     mv tex-fpc/tangle work/bin
     itgl dist/mf/mf.web tex-fpc/mf.ch
     itgl dist/tex/tex.web tex-fpc/tex.ch
     mv inimf initex work/bin
     mv tex.pool work/TeXformats
     mv mf.pool work/MFbases
  3. Update tex-fpc/local.mf to define printer settings. See comments in modes.mf for details. For example:

     % Brother HL-L2320D (name must be one to eight letters, lowercase)
     mode_def brhlld = 
        proofing := 0;
        fontmaking := 1;
        tracingtitles := 0;
        pixels_per_inch := 600;
        o_correction := 1;
     localfont := brhlld;
  4. Build the tex and weave programs.

     cd work
     inimf ../dist/lib/plain input ../tex-fpc/local dump
     mv plain.base MFbases
     cd ..
     sed -i '99,106s/\t//' tex-fpc/mf.ch
     tgl dist/mf/mf.web tex-fpc/mf.ch
     mv mf work/bin
     cd dist/mfware
     tgl gftopk.web ../../tex-fpc/gftopk.ch
     mv gftopk ../../work/bin
     cd ../../work
     cp ../dist/lib/hyphen.tex TeXinputs
     cp ../tex-fpc/webmac-fpc.tex TeXinputs
     initex ../dist/lib/plain \\dump
     mv plain.fmt TeXformats
     cd ..
     sed -i '198,204s/\t//' tex-fpc/tex.ch
     tgl dist/tex/tex.web tex-fpc/tex.ch
     mv tex work/bin
     tgl dist/web/weave.web tex-fpc/weave.ch 
     mv weave work/bin
  5. Build the xdvi program.

     cd tex-fpc/xdvi
     wget https://math.berkeley.edu/~vojta/xdvi/xdvi-22.86.tar.gz
     tar xf *gz
     rm *gz
     mkdir -p $TEX_HOME/xdvi/bin
     mkdir -p $TEX_HOME/xdvi/man/man1
     mkdir -p $TEX_HOME/xdvi/share/dvips/config
     cd xdvi*
     CPPFLAGS='-DBDPI=600 -DMAKEPK=4' ./configure --prefix=$TEX_HOME/xdvi \
       --with-default-texmf-path=$TEX_HOME/work/DVIPSconf \
       --disable-freetype \
       --enable-old-make-pk \
       --enable-extra-app-defaults=$TEX_HOME/work/DVIPSconf \
       --with-default-font-path=$TEX_HOME/work/PKfonts \
       --with-default-header-path=$TEX_HOME/work/DVIPSconf \
       --with-default-fig-path=.:$TEX_HOME/work/TeXinputs \
     make && make install
     cd ../../..
     mv xdvi/bin/xdvi* work/bin
     cd work
  6. Weave the TeX-FPC document.

     wve ../dist/tex/tex.web ../tex-fpc/tex.ch && xdvi tex

This results in:


See Wolfgang Helbig's TeX-FPC README file for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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