A Very Quick Guide to Using LaTeX in Algorithms

LaTeX: The Low Down


LaTeX is a language for specifying both the content of and the formatting directions for how a particular document should look. It is particularly useful when writing technical documentation because of the rich language it has for specifying equations. In what follows, some of the basic syntax is described (many of the examples, or slight modifications of them, are also realized in example.tex).

Probably the most efficient way to learn LaTeX in this class will be to:

For those of you who are dying to learn more than is offered in this very modest expose, there are many good resources: 1, 2, etc.


The Brass Tacks: Compiling a postscript file on Turing

First, get a copy of the example.tex file I've made to go along with this tutorial. This can be done by issuing the command:
cp /home/infosys/www/courses/2004/fall/cs140/homework/example.tex .
or downloading from the link above. Also copy over the little script, make_doc, that I've written to Latex and convert a latex file into postscript:
cp /home/infosys/www/courses/2004/fall/cs140/homework/make_doc .
To execute this, make sure it has executable permissions (you might need to chmod u+x make_doc). Then type:
make_doc example
Since the example file has no syntax errors (I checked!), the script will end its run with:
Your file: example.tex has been converted into example.ps
To view this file, type
gv example.ps
You can print the standard way:
lpr example.ps

With respect to modifying a particular homework assignment, you can skip the gorey details that go along with initially setting up a LaTeX document. Simply modify the one that I used to create the assignment in the first place. This can be obtained by downloading from a web-browser here or issuing the appropriate copy command, e.g. if you wanted HW1a, you'd type:

cp /home/infosys/www/courses/2004/spring/cs140/homework/hw1a.tex .
When modifying this file, don't forget to add your name! (One very good place to do this is between the "\begin{center}" and the next "\bf".)

How to recover from an Error

Suppose you're modifying a file and you screw up. For example, this would happen if you were to insert on the fifth line of example.tex the following:
\item 
Then when you run the make command, you'll get
See the LaTeX manual or LaTeX Companion for explanation.
Type  H return  for immediate help.
 ...                                              
                                                  
l.6 T
     his is just a stupid little LaTeX document.
? 
What this tells you is that it thinks the problem is at the first "T" in line 6. One point to take away from this is that LaTeX doesn't always identify perfectly where the problem is. So if you get an error like this, you might have to carefully think about what you've done to the file (and ask a LaTeX savvy friend for help if you find yourself spending too much time). Usually, the problem will be one of an ill-formed environment (described later), so that's the first thing to carefully inspect. For now, however, I'll just tell you how to get out of this mode. At the
?
you want to type an
x
This will tell latex to quit. Find your error, fix it, and then reissue the do_make.


Some Basic Commands:

Most LaTeX commands begin with a "\" and many of them take arguments. For example, "\item" is a LaTeX command that tells LaTeX to make a new item within an itemized list environment. On the other hand, "\begin{itemize}" tells LaTeX to enter into just such an environment. When arguments are used, they must be enclosed within a pair of brackets to be well formed.

I often use commands that specify what font LaTeX should use. For instance, if you want to display a phrase in bold, you could use

{\bf A phrase}
Similarly, italics could be specified with
{\em Another phrase}
In these cases, the commands go inside the curly braces (and these indicate the scope of words that should have a particular font).

Interestingly, "\\" is also a LaTeX command; it tells LaTeX to make a new line. It is also very useful to be able to skip downwards some fixed amount. For example,

\vspace*{3in} \\
will leave 3 inches of white space before continuing with the text. In the past, students have used this command to allow them to draw a figure by hand to accompany some homework solution writeup. I encourage you to use pictures whenever they are useful in clarifying your explanation.


Basic environments:

Just as blocks of code in a programming language are scoped, so are the formatting commands used in LaTeX. We will refer to any such block as an environment.

It is essential that blocks be well formed -- i.e. they must indicate where their environment begins and ends. There is different syntax for doing this depending upon what you are trying to do. For newbies, the most frustrating part of using LaTeX is trying to find out why your document won't "parse" -- especially since LaTeX's comments are not always as useful as one might like. Usually, the culprit is an ill-formed environment. Should your document not compile, check carefully near the lines it is complaining about to see what scope you neglected to close.

The environments you'll most likely see in this course's LaTeX files are described in the following sections.


List Environments

Enumerated (itemized) lists are specified in LaTeX as follows:
\begin{enumerate}
\item I'm the first item!
\item I'm the second.
\item and so on...
\end{enumerate}
Here "\begin" and "\end" define the environment.

A bulleted list is very similar:

\begin{itemize}
\item ...
\end{itemize}

Lists can be nested.


Math Mode

The mathematics environment -- i.e. math mode -- provides a wealth of commands for formatting mathematical equations. For example, many commands for displaying mathematical symbols exist, as detailed here. Some of the most common symbols used in this class include:
\sum
\prod
\infty
\subset
\subseteq
\in
\forall
\exists
\emptyset
\Leftrightarrow
\Rightarrow
\rightarrow
\leq
\geq
\Theta
\Omega
I should also call your attention to the Functions Table provided in the above link. One that you will want to use is "\log". For example:
$\log(x)$
is preferable to
$log(x)$
because in the former, the "log" is treated as a function name (displaying it in Roman type), whereas in the latter case, LaTeX treats the "log" as a variable (italicizing it).

To use any of the math commands, you must first enter into the mathematics environment.

"$" is used to delimit the math environment within a current line (this allows an equation to occur within a block of text). For example,

...equation $x = y + z$ is very useful because...
Note the "$" must occur twice; the first signifies the beginning of math mode and the second signifies its end.

If you wanted the equation to be centered on its own line, you'd use

\[ x = y + z \]

Another very powerful aspect of math mode is its ability to format symbols and text as sub- and super-scripts Algorithms-useful syntax includes:

And now, to clarify matters with a couple examples. Suppose that we had a vector x, and we wanted to assign a value of z to the y-th power to its i-th component. Here's one way to do it in LaTeX:
\[ {x}_{i} = {z}^{y} \]
Here's another:
\[ x_i = z^y \]
Now lets consider how to format the equation for specifying the Euclidean distance of some n-dimensional vector x:
\[ ( \sum_{i=1}^{n} (x_i)^2 )^{\frac{1}{2}} \]
Notice here that I used "\frac{1}{2}", which tells LaTeX to display the fraction one-half (since this occurs as a super-script, it is interpreted as the square-root).


A code-displaying Environment

The following environment is good for displaying code.
\begin{verbatim}
...
\end{verbatim}
In particular, in this mode, text is formatted exactly as it appears in the LaTeX file (white spaces, etc., are preserved).