[Linux Inside]

Linux Inside: The TeX Alternative

by Gene Wilburn

(The Computer Paper, November 1999. Copyright © Wilburn Communications Ltd. All rights reserved)


When Unix was first created by Ken Thompson in the late 1960's, it was quickly pressed into service at AT&T labs for document processing. In the days before desktop word processors, text processing was an important and common task for computers. Documentation was created by inserting tags and codes into a plain-text file, much in the manner of today's HTML, then "run off" through a specialized program to produce output.

In addition to sending output to printers, many of the text processing systems, such as troff (typesetting runoff), were designed to send output to a typesetting machine. Because of this typesetting bias, documentation produced with these systems is strikingly readable and often very beautiful, typographically.

One of the most brilliant text processing systems was created by Donald E. Knuth, a world authority on programming algorithms. He called his system TeX, pronounced 'TEK' or 'TEKHHH' with a Scottish guttural at the end. (Don't practice pronouncing it this way in polite company.) The final 'X' is the Greek letter 'chi' rather than the English letter 'X'.

TeX was created with mathematics in mind and it is still the premier system for producing books and articles that require complex typeset mathematical formulae. It also happens to be a very fine general-purpose typesetting system.

Typesetting languages, at a low level, are inherently complex and learning to typeset in either troff or plain TeX can rapidly reach guru levels. To make TeX much easier for writers, Leslie Lamport created a set of easy-to-use macros for TeX which he dubbed LaTeX (pronounced 'LAY-tek').

These mature, powerful typesetting systems are available for free with Linux and are included with every CD distribution. If you have enough disk space, you can treat yourself to a whole new adventure by installing them and trying them out.

The Linux troff program is actually called groff, for GNU runoff. Troff has gone somewhat out of favour as a typesetting system. With a few exceptions, these days it is used primarily to create man pages. TeX and LaTeX, however, are still widely used alternatives to word processing and there is an active world-wide TeX community developing a continuous stream of new features and macros.

One question always arises from those who have never been exposed to a text processing system before: if you can use a program like WordPerfect, why use something like LaTeX for documents?

There are some good fundamental reasons why you might want to try out the LaTeX alternative to word processing. First, it's free and it's open source. It's also portable across platforms--TeX and LaTeX are available for Unix, Macintosh, and Windows. What's particularly interesting about TeX is that for a bit more work up front, you get a bigger payoff at output time.

There are also aesthetic considerations. Word processors are sloppy. WYSIWYG programs have dumbed us down in terms of classical typesetting principles and conventions. Few people are taught how to use a word processor correctly, so output generally comes out however it gets slammed in.

With LaTeX, the emphasis is on organization rather than on looks. The built-in styles guarantee that your output will look highly professional and follow proper typesetting etiquette. All you have to do is enter the tags and you'll have great, classic-looking output.

Not only that, but Linux allows you to have your cake and eat it too. You can create LaTeX documents in your favourite text editor and also enjoy a beautiful WYSIWYG preview of the output, using a program called xdvi.

Here's an example of a simple LaTeX letter--the kind that might have been written in the days before email:

    
    \documentstyle{letter}
    \address{123 Linux Drive \\ Port Debian, ON \\ L5G 9X9}
    \signature{Ima Texhead, Jr.}
    
    \begin{document}
    \begin{letter}{Ima Texhead, Sr. \\
                   456 BSD Way \\
                   Berkeley, CA 95587}
    
    \opening{Dear Dad,}
    
    Hope you're proud to see I'm now using \LaTeX\, just like you, Dad.
    Now that I've arrived, I could use some cash for a new Linux system.
    
    \closing{Thanks,}
    
    \end{letter}
    \end{document}
    
    
    

TeX and LaTeX files are normally created with a '.tex' extension. If you use the Emacs editor on a file with this extension, it automatically goes into TeX mode and provides additional menu items to make TeX and LaTeX even easier.

Although the tags seem unusual at first glance, they bear a strong resemblance to HTML tags. Anyone experienced in hand coding web pages can enter the LaTeX mindset easily.

Let's call this letter myletter.tex. To preview it (you need to be in X Window for this) you type latex myletter.tex. If this finishes without errors, then type xdvi myletter.dvi &, wait a bit for the fonts to be generated, then watch a beautiful typeset-quality preview appear.

To send the output to a printer, you type dvilj myletter.dvi ; lpr myletter.lj if you have a LaserJet printer or equivalent or dvips myletter.dvi ; lpr myletter.ps to send the output to a Postscript printer. These programs convert the TeX device independent file (dvi) into LaserJet or Postscript files, respectively.

Post Processing

One of the things that makes LaTeX attractive are the number of post-processing programs available. One widely-available program, latex2html, can turn a book-length set of LaTeX files into a complete WWW-based online book, with table of contents, chapter and section breakouts, footnotes, index and bibliography.

A companion system to TeX, called BibTeX, provides a method of creating complex bibliographic citation databases and inserting relevant entries into footnotes and bibliography sections.

LaTeX also provides a very effective way to publish material from a database. LaTeX allows for the inclusion of figures and illustrations, making it ideal for creating things like beautifully typeset print catalogues from a database. Toss out Quark. You're about to experience LaTeX power!

CTAN

You can, of course, adjust LaTeX to follow different styles than the built-in ones, but you don't have to invent your own macros. A basic rule of thumb for Linux is "don't re-invent the wheel." Part of living in this exciting open-source community is that efforts are pooled together for community use.

Take a trip to Comprehensive TeX Archive Network (CTAN) at http://tug2.cs.umb.edu/ctan/. There you will find a couple of decades worth of LaTeX macros, or scripts, for everything from alternative style sheets to typesetting musical scores to creating business cards. The material is free.

If you get hooked on LaTeX and need help at times, join the newsgroup comp.text.tex, one of the friendlier and more helpful newsgroups on the Internet. Stick with LaTeX for a year, and you qualify for an "I am a TeXhead" T shirt.

For further reading on TeX and LaTeX, the following two titles are classics: The TeXbookLaTeX--A Document Preparation System, 2nd ed by Leslie Lamport, Addison-Wesley, 1985 (ISBN: 0201529831). Additional recommendations include The LaTeX Companion (ISBN: 0201541998) and The LaTeX Graphics Companion (ISBN: 0201854694) also published by Addison-Wesley.

Note that these books are all expensive, ranging from $50 upwards. Before investing in expensive reference materials, check out the excellent free guides and tutorials available from CTAN.

LyX: Visual LaTeX

If LaTeX sounds interesting, but the idea of poking tags into a text file doesn't appeal to you, there's an exciting open-source product called LyX you might want to investigate. LyX is a visual, near-WYSIWYG editor for TeX and LaTeX. Think of it as LaTeX with training wheels.

LyX, and its variant KLyX (slightly adjusted to work seamlessly in the KDE environment), has a look and feel similar to a word processor. You use your mouse to open a file, pick a style, pick options from menus and then, when you're ready, you just type away. It even does the xdvi previewing steps for you from the menu bar so you don't have to type commands manually.

LyX is integrated with the ispell spelling checker and has all the usual modern amenities such as search and replace and undo. It also has a great guide and a great tutorial built right into the program. Serious TeXheads who do very complex things with their files will still prefer the roll-your-own text approach, but LyX is a treat for those who just want to create straightforward letters, articles, and reports.

If you like the professional look of typeset documents but prefer the ease of use of a word processor, LyX may be your ticket. In fact, LyX is so nice that it may just become one of your most compelling reasons for ditching that other operating system and running Linux full time.

If LyX or KLyX is not included with your distribution, you can download the source as a gzip'd tar file and compile it into your system. The source for LyX is located at www.lyx.org.

Gene Wilburn (gene@wilburn.ca) is a Toronto-based IT manager, musician and writer who operates a small farm of Linux servers.

-30-