You may have read that LaTeX is “really just TeX with macros”, and it is true, but is a simple statement that doesn’t explain why TeX and LaTeX are separate executables, and why documents that will typeset with TeX won’t typeset with LaTeX and vice versa. Having just added Plain TeX support to our iOS LaTeX editor, I will take this moment to explain the difference.
The difference between TeX and LaTeX
First, it is necessary to understand that TeX is not just a typesetter, it is a typesetter with an embedded scripting language, whose commands configure and control the behaviour of the typesetter. LaTeX and Plain TeX are two different customised typesetting environments, with their own commands and conventions, based on TeX.
That is to say when you type LaTeX you are running TeX, but before it loads your LaTeX source it first loads the latex.ltx
file, which defines all the familiar \section
, \documentclass
, \begin
, \end
, \ref
, \label
, … commands familiar to LaTeX users. From now on I will be careful to make a distinction between TeX, the executable and typesetting language, and Plain TeX, which was the first environment written in TeX, and defined in the plain.tex
file. Unfortunately Plain TeX has incorrectly become synonymous with TeX over the years which can lead to a lot of confusion.
Having clarified that point I should now correct a small lie in the previous, TeX does not actually load the latex.ltx
afresh each time it typesets a LaTeX file, it restores it’s internal memory of a precompiled version of latex.ltx
, known as the format file. The version of latex.ltx
included in the iOS version of Texifier, our TeX editor is 8000 lines long. Parsing and executing this file just to typeset a 10 line document would make LaTeX prohibitively slow, especially on the 1980’s era CPUs for which TeX was designed.
TeX’s solution to this is to run through latex.ltx
, then dump it’s internal internal memory to disk in a format file, conventionally latex.fmt
. Now when you execute LaTeX it quickly restores it’s internal state from latex.fmt
, and processes your file. When you execute TeX it loads the state stored in the plain.fmt
file, which is created from plain.tex
. You can generate these files for yourself by running
initex *latex.ltx
or
initex *plain.tex
but this is a job best left to your TeX distribution’s installation scripts (or us if you are using our apps).
A final mystery is why there are separate latex
and tex
binaries on your system, a question which becomes more puzzling when you poke around and discover that they are symlinked to the same binary. When TeX boots up it inspects the command used to run it, and then searches for the command.fmt
format file and restores its state from that before processing your document. If no format file is found then it defaults to plain.fmt
instead. Alternatively, many TeX distributions allow you to specify the format file with the -ini
command line option.
You can turn this behaviour to your advantage and roll your own LaTeX with your document class and commonly used packages preloaded. For example you would copy/paste latex.ltx
to something.ltx
, insert the \documentclass
and \usepackage
commands before the \dump
command in something.ltx
, then generate something.fmt
with initex. Once this is installed alongside your other format files, you would create a something
symlink in your tex bin pointing to your tex
binary, and use it to typeset your files. The speed of modern processors makes this somewhat unnecessary and most likely your custom format file would become broken every time there is an update to your distribution which changes the TeX binary the format file was created for.
Basic Example Documents
Hello World \bye
\documentclass[11pt]{article} \begin{document} Hello World! \end{document}
The difference between TeX, e-TeX, pdfTeX, XeTeX, luaTeX
So far I have described the difference between TeX and LaTeX, but not between LaTeX, XeTeX/XeLaTeX, pdfTeX/pdfLaTeX and luaLaTeX/luaTeX. First things first, the difference between pdfTeX and pdfLaTeX is the same as the difference between TeX and LaTeX described above, it is the same binary with a different format file.
The difference between TeX, pdfTeX, XeTeX, luaTeX is that they are different versions of the fundamental TeX executable. Having written TeX, Knuth was keen to freeze the source code and concentrate on fixing bugs rather than adding features. This approach worked for some time, but soon the DVI file format that TeX creates fell into disuse and was replaced by today’s PDF file format. This means that typesetting a document with TeX requires you to execute two programs, TeX to create the DVI file, then dvipdfmx
to convert that to a PDF file. This is complicated by the absence of any colour, hyperlink, drawing or image support in the DVI file format. These elements have to be embedded in the DVI file using so called special string s and then decoded by dvipdfmx
on the other end.
This approach is at best slow, and in practice error prone, so with no possibility of altering the official TeX source code to output PDF, the only way of rationalising the problem was to create a new pdfTeX fork of TeX, which directly generates PDF files. In fact this has been repeated a number of times now, and there is a whole family of TeX binaries.
- TeX: Knuth’s original, unused today
- e-TeX: The first major set of extensions to TeX, including right to left typesetting, and a common progenitor to all modern TeX’s, but also unused today.
- pdfTeX: a development of e-TeX which can generate either PDF or DVI output files. What we refer to today as LaTeX or TeX is in fact pdfTeX running in DVI mode, in which case these DVI files must then be converted to PDF with an external tool.
- XeTeX: a separate development of e-TeX with support for modern (i.e. TTF) fonts and with native Unicode character set support. This solves a lot of problems for languages whose character sets were not included in either ASCII or the common 8 bit extension of ASCII. Unfortunately it does not produce PDF directly, but
xdvi
files which it then converts to PDF for the user. - luaTeX: this is a further development of pdfTeX, with similar Unicode and TTF support to XeTeX (although reimplemented on top of different font libraries). However as a descendant of pdfTeX it has the advantage that it generates PDF directly. Additionally it embeds the
lua
scripting language as a cleaner, less verbose and more modern replacement for TeX’s own scripting language.