A Quick Latex Tutorial

The following is a very brief introduction to using LATEX (http://www.latex-project.org) in the GNU/Linux environment. The myriad details are best tracked down in one of the following documents.

Leslie Lamport, LATEX : A Document Preparation System : User's Guide and Reference Manual, 2e, Addison Wesley, 1994

Tobias Oetiker, Hubert Partl, Irene Hyna, and Elisabeth Schlegl, The Not So Short Introduction to LATEX$2_\mathcal{E}$, http://www.ctan.org/tex-archive/info/lshort

LATEX source code : .tex files

A LATEX document contains a \documentclass statement, sometimes followed by other initialization and customization commands, followed by the document contents sandwiched between \begin{document} and \end{document} statements. Save the following lines
\documentclass[12 pt]{article}

\begin{document}

\noindent
Hello, world! Latex can typeset equations like
\begin{equation}
\int^{2 \pi}_0 \sin^2 \theta d\theta = \frac{1}{2}
\end{equation}
very nicely!

\end{document}
in a text file called hello.tex, and you have a minimal LATEX source file.

Before you add very much code to your source file, you ought to run it through the LATEX program to locate possible errors and view the output, which brings us to the next topic...

Producing and viewing typeset documents : .dvi files

At the shell prompt, type
$ latex hello.tex

Here is the resulting set messages written to my terminal window by LATEX.

This is TeX, Version 3.14159 (Web2C 7.3.1)
(hello.tex
LaTeX2e <1998/12/01> patch level 1 
Babel <v3.6x> and hyphenation patterns for american, french, german, \
ngerman, italian, nohyphenation, loaded.
(/usr/share/texmf/tex/latex/base/article.cls 
Document Class: article 1999/01/07 v1.4a Standard LaTeX document class 
(/usr/share/texmf/tex/latex/base/size12.clo)) 
No file hello.aux. 
[1] (hello.aux) ) 
Output written on hello.dvi (1 page, 616 bytes). 
Transcript written on hello.log.

You should see something very similar to this. If you don't, check the syntax of your source file. Error messages are rather cryptic, but usually refer to a line number in the source file. For example, if I leave the closing bracket off of my \begin{document} statement, I get the following error message in addition to the other messages shown above.

Runaway argument?
{document 
! Paragraph ended before \begin was complete.
<to be read again> 
                   \par 
l.4 
    
?

The l.4 tells me the problem is near line 4, which is in the neighborhood of line 3. Faced with an error like this, LATEX gives me a ? prompt. If I type an h, I get some remarkably helpful feeback.

? h
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

?

I type an x at the ? prompt, add a } to my source file in the appropriate spot, and LATEX compiles the document without further complaint.

If you are following along with your own hello.tex file, you will find that LATEX has produced the additional files hello.aux, hello.log, and hello.dvi. The .dvi file contains the typeset document in a peculiar format (device independent format) that we can view using the xdvi program. The other two files are not of much interest to the average user. Simply type

$ xdvi hello.dvi

and you will see a digital representation of your typeset document, very similar to that shown in Figure 1.

Figure 1: The interesting portion of the .dvi output from LATEX shown by xdvi. The rest of the (mostly blank) page and the page number have been omitted.
\begin{figure}\par Hello, world! Latex can typeset equations like
\begin{equatio...
... \sin^2 \theta d\theta = \frac{1}{2}
\end{equation}very nicely!
\par\end{figure}

As you compose lengthier documents, it is useful to keep your favorite text editor, xdvi, and a shell window open all at once. Saving your source file, running LATEX in your shell window, and viewing changes in your typeset document with xdvi can all be done without closing any windows. In fact, xdvi automatically updates its display to reflect changes in the file it is viewing when you bring its window into focus with the mouse.

Printing typeset documents : .ps files

If you want to print your typeset document, you will need to convert your .dvi file into a .ps file (Postscript format) using dvips. If you want to simply print the file, type
$ dvips hello.dvi

This sends the Postscript translation directly to the printer without saving it.

If you would like to have the .ps file, use the -o command line option

$ dvips hello.dvi -o

This produces the file hello.ps. You can then use gv (Ghostview) to view and print your postscript file. It has the nice feature that it can print selected pages. If you would like to send a postscript file to the printer, use the lpr command

$ lpr hello.ps



Copyright © 2001-2003, Lewis A. Riley Updated Fri Jun 21 13:19:27 2002