LaTeX for the Slightly Timid

Are you used to the simplicity of your old DOS word processor, and afraid of the complexity of LaTeX? This article tells you how to write a beautiful letter---and gets you started using LaTeX.

by Kim Johnson

Linux Journal

October 1995

I admit it. LaTeX was not my first choice for a text formatter. I was pressured into using LaTeX by my (then) fiancÚ, and the fact that at the end of the semester, all the IBM PCs and the Macs were being used 24 hours a day, while the Unix terminals were almost totally free. Also, I had to reboot from Linux into DOS every time I wanted to use one of my favorite word processing programs on my fiancÚ's computer, which was no fun. So, armed with a topology assignment I wanted to type, I started to learn LaTeX.

Even after the first week or so of using LaTeX, it was not my favorite program. It wasn't a word processor or an editor---it was something new to me. I didn't see the italics or the underlining or my math equations on the screen as I entered or edited my papers. After a while, though, I got used to the rules, and my papers looked so beautiful that I fell in love with LaTeX. I am not a LaTeX or TeX (LaTeX is based on TeX) guru---it is all I can do to remember how to start a document. But the freedom and power of LaTeX make it well worth the initial time investment to learn it, and if you are doing ordinary everyday writing, LaTeX is not terribly difficult to use.

It Takes Some Work

There are a few major differences between LaTeX and your favorite word processor. It is not WYSIWYG (what you see is what you get) or WYSLRN (what you see looks really neat---bold is bold, italic is italic, but new lines and page breaks aren't necessarily where they will be when the document is printed). With LaTeX, what you see is what LaTeX sees, and you type in the exact commands that you want LaTeX to use. This means that WYSLRU (what you see looks really ugly) while you are entering your text. [There are so-called front ends to LaTeX that are almost WYSIWYG. Linux Journal will probably contain articles on those in later issues, because they can make it much easier for you to use LaTeX. ---ED] However, because what you see is exactly what LaTeX sees, if something in your document output doesn't look right, you can perhaps find it and fix it more easily than you could with a word processor.

Another difference is that when it comes to formatting decisions, LaTeX will handle the details if you handle the general idea. For example, if you want your footnotes labeled with letters instead of numbers, or the numbering of footnotes to begin at 1 on every page, or some other labeling scheme, you need only instruct LaTeX once at the beginning of your document. If you change your mind, you don't need to change all your footnotes---just change the instructions at the beginning, and LaTeX will handle the rest.

The last difference is that there is no standard user interface for LaTeX; you can use whatever text editor you want. I use Emacs, but any Emacs clone, or vi or another editor which uses plain text, will work. Because you can use any editor, starting an article or paper is a little more complex than in a word processor. You need to tell LaTeX the type of document you are creating, normally a "book", "article", "report", or "letter". Any book on LaTeX will explain the specific differences between the document types; their names should give a good idea of what they are for.

Writing a Letter

Having told LaTeX which type of document you are doing, it figures out the page formatting and the special options needed throughout the document. I will use the "letter" document style to demonstrate the basics of typing documents in LaTeX, as well as the power of LaTeX to make documents easy to format and beautiful.

To begin any document, after starting your editor you need to place a preamble at the start of the file, of the form:

\documentstyle[...]{...}
A few things need to be noted about this preamble. First, the backslash. LaTeX uses the backslash before most commands, so you should get familiar with its location on your keyboard. The brackets contain options that change something less drastic than the document style. For instance, you can change the type size from 10 point to 11 or 12 point, set the entire document in two columns, or use other more advanced options. The braces hold the name of the document type: usually article or letter. The braces and their contents are required.

Since we are writing a letter, we will start out with:

\documentstyle{letter}

All documents need \begin{document} at the beginning (preceded, of course, by the \documentstyle command and, often, by other more advanced commands) and \end{document} at the end.

Letters are unique among the styles. You may want to write several letters in the same file, reusing the same return address and signature. LaTeX allows you to do this by stating the address and signature before the \begin{document} statement. Then, each letter is begun with \begin{letter} and ended with \end{letter}. After all your letters, put \end{document} to end the document.

The format of the \address and \signature commands are similar and something you will become very familiar with in LaTeX. Type the command, followed by the argument (for these commands, your address and your name and title) in braces:

\address{Linux Journal\\P.O. Box 85867\\
Seattle, WA 98145--1867}
\signature{Kim Johnson\\Chief Bottle Washer}

Notice the double backslashes; these are added to force a new line---simply pressing return will not work, as I explain later.

Now, begin your first letter:

\begin{document}
\begin{letter}{Aunt Jane\\
St. Mary Mead\\England}
The argument in the second set of braces is the address of the recipient of the letter. Note the recurring double backslashes and the tendency to put arguments in braces to set them apart. You may want to add an \end{letter} at this point and then type the rest of the letter between the \begin and \end---it saves a lot of time debugging later.

The other options are mostly self-explanatory from the example letter.

\opening{Dearest Aunt Jane,}
This is a very short letter.
\closing{With love,}
\cc{Hercule Poirot\\Tuppence}
\encl
\ps{P.S. This is a postscript.}
\end{letter}
\end{document}
Your name is added underneath the closing of the letter from the \signature command that was used before the \begin{letter}, as shown in Figure 1.

Figure 1. Output from LaTeX

It would be possible to arrange this letter on the page "by hand" on most word processors, and even on LaTeX, but the point is LaTeX does the formatting and you do the writing. The other document styles do not format quite so aggressively, but they do allow you to add your title and dedications without having to worry about getting everything centered and on the correct page.

Outputting Your Document

Now, suppose that you want to see what your letter really looks like---you certainly don't want to send it as it appears in your letter.tex file. So, after saving the file and exiting the editor (or just popping over to another virtual console), from within the directory where letter.tex is, type latex letter.tex. LaTeX will format your file it in a way that can be displayed and printed. The formatted file will be called letter.dvi; DVI stands for "device independent", which means that your file can be displayed or printed using a number of programs. If you are using X-Windows, type xdvi letter.dvi to see what you have written. If you are not running X-Windows, you may be able to use dvgt, which comes with some distributions and is also available from sunsite.unc.edu in the /pub/Linux/apps/tex/dvi/ directory. However, its user interface is not the best possible for a novice, and xdvi is certainly easier to use.

Special Formatting Commands

"The text looks great!" you say, "But I didn't just want to type plain text in, I wanted to add italics and underlining and mathematical equations and footnotes and..." (If you didn't get past the command latex letter.tex, I'll give some hints for debugging your letter later.) Most of these commands are easy enough and very similar to the commands used so far.

For example, to create a footnote, type \footnote{This is the text of the footnote.} wherever you want the footnote to appear. Note that you will have to be careful with spacing---put the \footnote command after any punctuation, but before any trailing space. LaTeX will make a complete list of the footnotes and number them correctly. However, if you want to number them yourself, use \footnote[num]{text} instead. Remember that brackets contain optional things, so you don't need to number the footnotes yourself unless automatic numbering won't do what you want.

Creating italic and bold text is very similar, except the bold and italic commands are actually inside the braces instead of outside. This is so that more than one command can apply to a region. Here are some examples: I can make some words {\bf bold}, {\em italic}, {\em\bf bold-italic}, or {\em have a {\bf bold} word in a group of italic words}. (\em stands for emphasized text; \it, which stands for italic, works as well.) LaTeX has many other type styles which work the same as bold and italic; they are documented in all LaTeX reference books.

Another method used in LaTeX for text formatting is similar to the one you used for the letter environment with the \begin and \end commands. For example, here is how to center text:

\begin{center}
Here is my centered text,\\
here are two\\
more lines of centered text.
\end{center}
Again, as in the letter, the double backslashes signal a new line. The \begin and \end commands, along with {table}, {quotation} and many other options, are documented in the reference manuals.

To sum up, when dealing with some text which is to be put in a separate place on the page, as in \footnote and \opening, the text goes after the command in braces. When dealing with a few words in the flow of the main text, the command (like \em and \bf) goes within the brackets along with the text, to set it off from the text around it. And finally, when dealing with larger blocks of text within the document which need to be displayed specially, such as centered text or a table, commands such as \begin{center} and \end{center} are used around the text.

A note about line breaks and spacing: When LaTeX sees a line break in your typed-in text, it just assumes that your line got too long and you went to the next line to keep entering text for the same paragraph. LaTeX knows better than you do how many words fit on a line, so one line break just doesn't register with LaTeX. Two or more line breaks (one or more blank lines), on the other hand, are interpreted as a "new paragraph", so LaTeX will skip a line and/or indent, whatever is appropriate in your document style, and will not put that extra new line in the output. If you want to force LaTeX to break a line without a new paragraph, you must use a \\, a double backslash.

LaTeX also interprets extra spaces and extra empty lines just as it would one space or one empty line. You probably won't know exactly how many lines to leave blank; just leave it up to LaTeX. As in the letter above, LaTeX knows where on the page to put everything so it looks good, so let it do the work. On the other hand, if LaTeX does not know how to make things look right, you do have some control. As above, use \\ (or equivalently, \linebreak) to specify a line break, and \pagebreak to begin a new page. If this doesn't work, see one of the books about it, or else ask your local "TeXnician" (TeX-speak for "TeX guru").

Finally, you can add horizontal and vertical space with \hspace{width} and \vspace{height} respectively, where width and height refer to the amount of space you want added. For example, \hspace{.25in} would make the current line be at least a quarter inch high; it's the equivalent of an infinitely thin letter a quarter of an inch high. This may not work at the beginnings of paragraphs, the end of lines, the end of pages, and various other spots. Again, see the book if you can't get it to work the way you want!

But It Didn't Work

Now, back to your letter. Suppose that you typed it all in, tried to LaTeX it, and it didn't work. This happens to everyone once in a while. To get back to the prompt after a failed LaTeX run, type q or x or ctrl-d (this is one of my favorite features of TeX---it's very easy to get out of). A number of common problems cause errors. First, did you remember to put an \end{document} at the end of your file? Or are there any other \begin statements without a matching \end statement? A variation of this is missing or mismatched braces or brackets. Another common problem is misspelled commands---if you tell LaTeX to "senter", it doesn't know what to do. My most common problem using forward slashes instead of backslashes, so I routinely do a search-and-replace for forward slashes with backslashes before printing. Also, I use Emacs in latex-mode, so it warns me about mismatched braces.

Finally, LaTeX has some special characters which can't be used in text as they are, because they have special meanings (which aren't covered in this article). These characters are:

# $ % & _ { } ~ ^ < > \
All of these characters can be produced by LaTeX. The first seven can be produced by typing \#, \$, \%, \&, \_, \{, and \}, and the rest can be produced by typing \verb+~+, \verb+^+, \verb+<+, \verb+>+, and \verb+\+.

LaTeX's error messages are usually helpful, giving a line number near where the error may have occurred and the error type. They are not always correct, though, so you must do a bit of work, but since everything is out in the open, it is usually not too onerous. I recommend frequently saving and LaTeXing your work, so you catch errors quickly and not after your 3000 word report is finished (or so you thought). It is also useful to display your work frequently, and if you are using X-Windows, you can iconify the xdvi window when you are entering text or LaTeXing. xdvi will automatically use the newest version of the dvi file, so you don't need to exit and restart xdvi over and over.

Printing

When you are finished editing and are ready to print, you have a few options. If you have a PostScript printer, the easiest way to print is probably to use the dvips program that should have come with your TeX/LaTeX installation. If you have the lpr program installed, the command:

dvips filename.dvi | lpr
will print the file. If you want to create a PostScript file, you can instead type:
dvips filename.dvi -o filename.dvi

Other programs are available for other printers. A series of drivers for the HP LaserJet printers are commonly used; for a LaserJet 4, you might type:

dvilj4 -e- filename.dvi | lpr
to print. The documentation for the LaserJet drivers is available by typing man dvilj.

If you have any other printer, it is probably supported by the Ghostscript program. Ghostscript is a PostScript language interpreter included in most Linux distributions. Setting it up is beyond the scope of this article, but once it is set up, you can use dvips to create a PostScript file and Ghostscript to print it. For instance, if you have a printer which is compatible with the HP DeskJet 500, you can run:

gs -q -sDEVICE=djet500 filename.ps quit
A full list of devices is available by typing:
gs
devicenames ==
quit
Don't use the "/" preceding the device names.

If your printer supports printing at different resolutions, you may be able to specify the resolution on the gs command line as well. For example, for 240 pixels per inch by 72 pixels per inch, add -r240x72 to the command line before the filename. More information about Ghostscript may be obtained by running man gs.

Warning: Do not just type lpr letter.dvi to print your letter; it will most likely be a mess. Most Linux distributions do not come set up to automatically print DVI files correctly. It is possible to install "print filters" which will make your life easier. One such program is called apsfilter and will be explained in a future article in Linux Journal.

Not Just Letters

You will probably want to do more than write letters. Other document styles are available, as mentioned earlier. This article is not the place to go into all of them, but I find "article" a good style to use in general. You can make a generic template document with the preamble and any other options you like consistently. Then, whenever you want to write a new document, copy that file to your new document, and you will be ready to go with no fuss.

Finally, good luck and have fun! LaTeX is not as difficult as some people make it seem, and the results after a little work can be truly dazzling.

LaTeX Resources

Kim Johnson is working towards her PhD in Mathematics at the University of North Carolina, Chapel Hill. When she isn't studying, she is usually reading a classic British mystery novel.

Copyright 1995