# Typesetting algorithms in LaTeX

## Pseudocode

Need to explain an algorithm? If you are permitted to list real source code, this is possible via the listings. I have yet to encounter a journal or conference that endorses this however.

Convention dictates explaining an algorithm with pseudocode. This obviates the problem of the uncertain semantics of particular programming languages, by replacing them with the semantics of no programming language at all. There is a confusing profusion of options for doing this and all options are, IMO, inadequate, since none of them allow me to typeset higher order functions naturally, and that is an old, thoroughly mainstream idea that I use a lot. This means, for example, that it is hard to typeset automatic differentiation. But who would need that? Anyway, this is a sub problem which I can awkwardly work around like everyone else.

tl;dr: I use

• For formatting,

• algorithmicx + algpseudocode (a nice default syntax that comes with algorithmicx).
• Or algorithms which comes with only one syntax, but everyone knows it
• inside an algorithm float.

## algorithmicx

How this looks:

\usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}  % skip EndFor etc
\usepackage{algorithm}   % custom floats

then

\begin{algorithm}
\caption{Euclid’s algorithm}
\label{euclid}
\begin{algorithmic} % The argument is the first line number
\Procedure{Euclid}{$$a,b$$}
\Comment{The g.c.d. of a and b}
\State $$r\gets a \bmod b$$ \label{init}
\While{$$r\not=0$$}
\Comment{We have the answer if r is 0}
\State $$a \gets b$$
\State $$b \gets r$$
\State $$r \gets a \bmod b$$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $$b$$
\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}

Algorithm line label references look like

\algref{euclid}{init}

If I am running minimalist TeX I need

tlmgr install algorithmicx algorithms

or I can do without algorithms if I do

\usepackage{float}
\newfloat{algorithm}{t}{lop}

Or perhaps I wish to typeset real code in a real language? minted enables this, allowing colour-highlighted math-enabled code rendering, bringing to LaTeX documents the conveniences that have been available to everyone else for some decades now.

There are alternatives.

## algorithms

algorithms provides algorithmic and an algorithm float, and seems to be fairly common and also fairly interchangeable with algorithmicx. The making your own floats does not need a package, but the algorithmic markup is non-trivial.

This seems intermittently maintained but has better reference syntax, in that referring to a line number just works - \autoref{alg:euclif:init} does what you would expect. It also seems to be preferred by IEEE.

TBD.

## program

program does pseudocode formatting but with substantively different syntax and styling. It looks nice but I don’t use it as it’s less compatible with the other so the odds of being able to copy and paste are low and copy-and-pasting ancient code snippets is how academia works.

### No comments yet. Why not leave one?

GitHub-flavored Markdown & a sane subset of HTML is supported.