Helpful LaTeX things I wish I had known a long time ago

(Back to my home page).


How to do pseudocode in LaTeX

Contrary to my earlier understanding, you can get LaTeX style files that do a nice job of pseudocode in a standard, Algol-like style. I find using them to be pretty easy, e.g. you just say something like:
\IF{$P=NP$}
\STATE Run algorithm
\STATE Print result
\ELSE
\STATE print ``Sorry, that would take too long''
\ENDIF

My source of these is one particular mirror of The TeX Catalogue Online, Complete CTAN Edition (there seem to be tons of these on the web). NOTE: the versions of these files on my page might not be as up-to-date as the official CTAN versions.

Downloadable files for algorithm styles

I've found some different LaTeX style files that are all reasonable choices. Note that in some TeX systems, these may already be installed.

An example of the one I usually use, algorithmic.sty, along with Visio & some other stuff is here (it was for students taking an introductory theory of computation class I TA'd).

Installing

To install either of the above files that I have ('algorithmic.sty' or 'alg.sty'), untar/gzip them like so:
Get into an good directory, type
gzip -d -c algorithms.tar.gz | tar xvf -

Then you need to make sure your LaTeX system looks in those directories for .sty files (generally, if you put it under your main TeX directory, it'll work, but I'm no TeX expert). Other than that, you're done.

Example usage/docs

Both come with .ps files that explain how to use it, and give examples. You'll see the file once you unpack the .tgz file.

Here's an example of mine for the algorithmic.sty file, in case your as LaTeX-naive as me:

\documentclass{article}
\usepackage{algorithmic}
\begin{document} 
\title{Example algorithm document}
\author{Zasha Weinberg (zasha@cs)}
\maketitle
Consider the following algorithm for sorting.  
The call $SlowSort(A,i,j)$ sorts the items
$A[i]\ldots A[j]$.

{\bf procedure} $SlowSort(A,i,j)$
\begin{algorithmic}[1]
\IF{$i\geq j$}
	\STATE Return
\ENDIF
\STATE $m\gets \lfloor (i+j)/2 \rfloor$
\STATE $SlowSort(A,i,m)$
\STATE $SlowSort(A,m+1,j)$
\IF{$A[m]>A[j]$}
	\STATE exchange $A[j],A[m]$
\ENDIF
\STATE $SlowSort(A,i,j-1)$
\end{algorithmic}

(This algorithm is due to Broder and Stolfi 
[SIGACT News, Fall, 1984]).
\end{document}
A more complete example along with Visio & some other stuff is here (it was for students taking an introductory theory of computation class I TA'd).
zasha@cs.washington.edu