NIU Department of Mathematical Sciences
Introduction to TeX

STUCK? SEARCH INDEX MAIN

The philosophy of TeX

TeX, created in the 80's by Donald Knuth, has become a standard for scientific document creation. It is not a word processor, but a typesetting system. It resembles a complex programming language more than anything.

Plain TeX was quite hard to learn, but it was designed to be easily extensible. Leslie Lamport created a set of commands which "ride on top of" TeX, and are known as LaTeX. They make it easier to create typical TeX documents without getting too far into the intricacies and quirks of the original system. In everything that follows we will concentrate on LaTeX, rather than plain TeX.

The main idea is to think about all elements of a printed page (the entire page, the paragraphs, lines of text, a displayed formula for the summation of the diagonal elements of a matrix, the integral symbol, the letter "a") as things enclosed by rectangular boxes. Those boxes have dimensions depending on the size and the spacing of smaller boxes they contain. And all of them are held together by an invisible substance called "glue", much like "ether" held everything together in XIX-th century physics. Except TeX glue is real (programmed into the system), and can stretch or shrink somewhat depending on the circumstances.

The circumstances are so varied that it's mind-boggling. When you get to know TeX better, you'll be surprised that it works at all, let alone that it works so well so often. Scientific typing has its rules, some of them rigid but most meant to be broken. It's next to impossible to systematize and predict them all, but that's exactly what Knuth attempted to do. Needless to say, the algorithms that TeX uses are highly non-trivial.

When you create a TeX document, you are telling the TeX program to assemble this huge contraption consisting of boxes held together by glue into a nicely formatted printed page. Most of the time the algorithms devised by Knuth will do a very good job. But sometimes (usually because you break certain rules!) TeX will behave strangely, and then forcing it (especially LaTeX) to do what you think is right will be extremely frustrating.

The main rule of thumb when working with TeX is: let it do its thing! You can tame this beast only after reading through a few thick books full of obscure explanations and caveats.

Creating a TeX document

TeX source files are ordinary text documents which can be created and edited with any editor: vi, emacs, pico, nedit, cedit, and so on. If you work on a Mac or a PC you can use an editor or a word processor, but remember to save the file as text (ASCII) before TeX has a go at it. It will then have to be processed by TeX, and viewed on the screen or printed on paper.

TeX commands are keywords preceded by the backslash; they are often quite cryptic: \bf, \leftharpoonup, \relax. A TeX file can contain comments which will not be processed (in fact, you should use comments both to explain what you are doing, and for readability): on any line everything starting with a percent sign is ignored.

There are three main ingredients of a LaTeX file:

In LaTeX 2.09 the first line looks like this:

\documentstyle[option1,option2,...]{main style file}
The optional options (huh!) specify auxiliary files that TeX should read in before processing the document. Those are usually enhancements which - for example - allow you to include PostScript graphics easily, or they set certain parameters specific to the type size you want to use.

The main style file contains definitions of commands such as "chapter", "theorem", etc., and often sets the size of the printed page and so on. Those are different for a typical journal article, a personal letter, or a book, and that's why there are different style files such as "article", "book", and "letter". I suggest that you use "article" for your first experiments.

In LaTeX 2e the line looks like

\documentclass{main style file}
The various options are now known as "packages", and you have to say
\usepackage{epsf}
\usepackage{doublespace}
and so on in the preamble. Other than that, there are only a few differences between the two versions. LaTeX 2e has some significant improvements over the old version.

The preamble of the document starts right after the documentclass or consists of various customizations of the way LaTeX normally works. You can put definitions of your own commands there, you can tell TeX not to print page numbers, etc. The very basic preamble might look like this:

\textheight=9in \topmargin=-0.2in
\textwidth=6.7in \oddsidemargin=0.2in \evensidemargin=0.2in 
You can read more about these incantations in Lamport's book mentioned below.

The document itself starts when LaTeX encounters the phrase

\begin{document}
and continues until it sees
\end{document}

You will see many other uses of such \begin{something} ... \end{something} pairs, which in LaTeX slang are called "environments". Think about them as brackets which must be properly balanced. They are not present in ordinary TeX.

To summarize, let's look at a trivial LaTeX source file with all the necessary ingredients:

\documentclass{article}
% this is a comment; we also show a command which belongs in the preamble:
\textwidth 4in % width of the printed page
\begin{document}
Let's type some text here;      TeX doesn't care about spacing
and line breaks -- it will do whatever it wants anyway...

New paragraph is started with a blank line.
\end{document}

After you've read about processing TeX files, you may want to copy this into a file and look at what comes out.

Processing TeX files

There are several steps here, and you will in all likelihood have to repeat them until you get things right. The details apply to the Unix version; on a Mac or under Windows you would use menus in some of the steps.

  1. Edit the TeX source and save it, e.g. as mydoc.tex (the extension .tex is customary, and some programs even require it, so we might as well use that)
  2. Process the source: type latex mydoc.tex; TeX will start assembling the pages, and it might give you errors. If it gets stuck in the middle of the document, you can press Return to try going further, or hit X to stop it, or finally E to go into the editor and fix the problem right away. If it complains that it cannot find a file (e.g. a style file you requested), you should press Control and D at the same time.
  3. If TeX managed to process the document, it will create a file mydoc.dvi. It contains the positions of all the boxes (remember the glue?) in which letters and symbols will appear. But it does not have in it the specifics of how to render the images - dvi stands for "device independent".
  4. "Flesh out" the document - print it or view it on the screen. If you are using X-Windows, the command xdvi mydoc.dvi will put up an image in a separate window. On most Unix systems dvips mydoc.dvi will print it on the default laser printer (in our Department you should use mprint mydoc.dvi to route it to the right place). This is the stage when the actual shapes of letters in various fonts in various sizes are incorporated, so you may still get errors if a font is missing, or you may see an auxiliary font creation program kick in).
LaTeX includes powerful and useful mechannisms for automatically referring to other parts of the document: page numbers, equations, sections. Since those cannot always be resolved until LaTeX has processed the file once already, step 2 should usually be repeated twice. In simple documents this isn't necessary, but you may want to start getting into this habit. See a separate section for details.

Modes

TeX has two major "modes": the default one in which it processes regular text, and one in which it typesets formulas ("math mode"). Certain things work differently in these two circumstances.

While typing ordinary regular text, remember that TeX is using its own settings for paragraph indentations, line spacing, spacing between words and letters (remember the glue?) and so on. Until you learn more, it is not a good idea to try and force things to look different. For example, no matter how many spaces or tabs you type between words, TeX will put them right next to each other. And to start a new paragraph, you have to put in one blank line by pressing 'Return' twice.

To get into the math mode, you type the dollar sign, and end it with another one. All of a sudden spaces are pretty much ignored, the typeface changes somewhat, and you can access zillions of mathematical symbols. There is also a "double dollar" math mode which places the formula in the center of the page and adds a bit of spacing on top and bottom - this is called "display math mode", but mostly it behaves like the regular one.

Math mode ignores nearly all spaces you type, makes letters appear slanted (as is customary for variables etc.), and applies special rules to spacing between symbols - e.g. the equality sign will be surrounded by a little extra space. It also lets you use subscripts and superscripts, and a plethora of special symbols used in scientific documents.

Hints

There are several rules of good TeX'ing. Following them can save you a lot of time and effort. Here are a few of them:

Further reading

The standard reference for TeX is the 'blue book' by Donald Knuth (The TeX book, Addison Wesley). LaTeX is described in detail in Leslie Lamport's LaTeX, also published by A-W. There are several other texts which claim to be easier to swallow, but we suggest Lamport's book as an essential first purchase for anyone who plans to use LaTeX on a regular basis.


Last modified: 8/20/97 by webmaster@math.niu.edu