As I mentioned a few weeks ago, one of the more popular and capable typesetting tools for Linux is TeX. It comes with all of the major Linux distributions. The TeX system itself, a large collection of files and data, is packaged in distributions; TeTeX is the TeX distribution designed for Linux, and you can find it on the Web at http://www.tug.org/teTeX/.
Donald Knuth, the world's foremost authority on algorithms, wrote TeX in 1984 as a way to typeset his books, because he wasn't satisfied with the quality of available systems. Since then, many extensions to the TeX formatting language have been made -- the most notable being Leslie Lamport's LaTeX, which is designed to facilitate the typesetting of structured documents. (LaTeX probably gets more day-to-day use than the plain TeX format, but in my experience, both systems are useful for different kinds of documents.)
While not everyone wants needs to write documents with TeX or LaTeX, these formats are widely used, especially on Linux systems. So every Linux user has the potential to encounter one of these files. This column tells you how to deal with them.
When someone writes a document with TeX, they compose an input file, which is a plain text file that contains the text of the document, with the TeX formatting commands interspersed in it.
So first, we'll need to get some sample input files to process.
For the following examples, we'll use some tutorial files written on the subject of learning TeX and LaTeX itself -- Michael Doob's A Gentle Introduction to TeX and Tobias Oetiker's The Not So Short Introduction to LaTeX. These tutorials are available on the Net in the respective formats they describe. You can download them here:
The TeX input for A Gentle Introduction to TeX is one file, called gentle.tex.
The Not So Short Introduction to LaTeX is a somewhat larger document, and its chapters have been put in separate files. So the LaTeX input is a collection of several files in a .zip archive -- after you download it, use the unzip tool to extract all of the files:
$ unzip src.zip
This command makes a new subdirectory, called src, which contains all of the LaTeX input for this document.
To view or print a TeX or LaTeX document, you process its input file.
TeX and LaTeX files are processed with different commands. So, the first step is figuring out which format a file is in.
By convention, TeX files always have a .tex file name extension.
LaTeX input files sometimes have a .latex or .ltx file name extension instead -- but not always. One way to tell if a .tex file is actually in the LaTeX format is to use grep to search the file for the text "\document", which every LaTeX (and not TeX) document will have. (The regular expresion to use with grep is '\\document', since slash characters must be specified with two slashes.)
For example, to determine whether the file "gentle.tex" is a plain TeX or LaTeX document, type:
$ grep '\\document' gentle.tex RET $
In this example, grep returned to the shell prompt without matching and printing any lines at all, so you can safely assume that the file gentle.tex is a plain TeX document.
Use the tex command to process TeX files. It takes as an argument the name of the TeX source file to process, and it writes an output file in DVI ("Device Independent") format with the same base file name as the source file, but with a .dvi extension.
To process the TeX file gentle.tex, type:
$ tex gentle.tex RET
That's all there is to it! The output is written to a DVI file named gentle.dvi. (During processing, TeX makes other files, too -- for example, you'll find a gentle.log file, which will contain a log of error messages, if any; you can safely ignore these files -- and delete them if you wish.)
The latex command works just like tex, but you use it to process LaTeX files.
The Oetiker text consists of several separate LaTeX files in the src directory, with the file lshort.tex being the base file for the document -- when you process this file, LaTeX will read and include all of its subsidiary files.
To prepare the LaTeX file lshort.tex, type:
$ latex lshort.tex RET
This command outputs a DVI file, lshort.dvi.
You may need to run latex on a file several times consecutively -- LaTeX documents sometimes have indices and cross references which, because of the way that LaTeX works, take another run through latex to fully process.
So to make sure all of the cross references in this document have been generated, run latex on it once again:
$ latex lshort.tex RET
The lshort.dvi file will be rewritten with an updated version containing the proper page numbers in the cross reference and index entries.
Ok, so you've successfully made a DVI output file from your TeX or LaTeX input. Now what?
Once you've produced a DVI output file, you can preview it on the screen, print it out, or convert it to another file format.
Use the xdvi tool to preview it on the screen. It shows how the document will look should you print it, and lets you view it at different magnifications.
For example, to preview the DVI file lshort.dvi, type:
$ xdvi lshort.dvi RET
This command opens xdvi in a new window and displays the first page of the document; on the xdvi toolbar you'll find buttons for enlarging the view, moving through pages, and exiting the viewer.
If you have a printer installed on your system, you can print the DVI file. The dvips tool will convert the DVI file to PostScript, and spool it to the printer (most systems with non-PostScript printers are set up with filters to convert the PostScript to a format the printer understands).
To print the file gentle.dvi, type:
$ dvips gentle.dvi RET
Instead of sending the PostScript output to the printer, you can
write it to a file. Give the name of the file with the
To write a PostScript file lshort.ps from the file lshort.dvi, type:
$ dvips -o lshort.ps lshort.dvi RET
This writes a new file in PostScript format, lshort.ps; it doesn't alter the original DVI file.
The ps2pdf tool, part of the gs (Ghostscript) package, converts a PostScript file to PDF. Give as arguments the name of the PostScript file, and the name of the PDF file to write to. For example:
$ ps2pdf lshort.ps lshort.pdf RET
This writes a new file in PDF format called lshort.pdf. The original file lshort.ps is not altered.
To convert the PostScript output to plain text, use ps2ascii, which works just like ps2pdf but outputs a file in plain text:
$ ps2ascii lshort.ps lshort.txt RET
This command converts the PostScript file to plain text and writes it to lshort.txt. (You can also use the gs tool to convert the PostScript output to an output format suitable for another printer or output device.)
Next week: for those who want to dive into the world of TeX, some document templates to make it easier.
Michael Stutz was one of the first reporters to cover Linux and the free software movement in the mainstream press.
Read more Living Linux columns.
Discuss this article in the O'Reilly Network Linux Forum.
Return to the Linux DevCenter.
Copyright © 2009 O'Reilly Media, Inc.