# What is a good package for displaying algorithms?

http://en.wikibooks.org/wiki/LaTeX/Algorithms_and_Pseudocode

I'm wondering which one to use. Any suggestions? I'm using other packages too so whichever is less likely to interfere with them is better for me.

• I would suggest you change the title to something less subjective, such as "What is a good package for displaying algorithms?" or more in line with the current one "What is a good package for displaying dem algorithms?" :) Aug 9, 2010 at 7:28
• – Werner
Jun 4, 2014 at 22:05

Personally, I favour algpseudocode from the algorithmicx (note the trailing x!) package. With a bit of setup, this can be harnessed to create beautiful pseudocode.

This was produced by the following code:

\begin{algorithm}
\caption{Counting mismatches between two packed \DNA{} strings
\label{alg:packed-dna-hamming}}
\begin{algorithmic}[1]
\Require{$x$ and $y$ are packed \DNA{} strings of equal length $n$}
\Statex
\Function{Distance}{$x, y$}
\Let{$z$}{$x \oplus y$} \Comment{$\oplus$: bitwise exclusive-or}
\Let{$\delta$}{$0$}
\For{$i \gets 1 \textrm{ to } n$}
\If{$z_i \neq 0$}
\Let{$\delta$}{$\delta + 1$}
\EndIf
\EndFor
\State \Return{$\delta$}
\EndFunction
\end{algorithmic}
\end{algorithm}


And used the following setup (this is just an example to replicate the above; you can of course use your own setup):

\usepackage{fontspec}
\setmainfont{Hoefler Text}
\newcommand*\DNA{\textsc{dna}}

\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\alglinenumber[1]{
\algrenewcommand\algorithmicrequire{\textbf{Precondition:}}
\algrenewcommand\algorithmicensure{\textbf{Postcondition:}}


To get the end-less pseudocodes, I included the package as follows:

\usepackage[noend]{algpseudocode}


The above pseudo code is nested inside an algorithm float environment. This environment isn’t part of algorithmicx. Instead, you need to load the package algorithm to get it. To produce a list of all algorithms, you can use

\listofalgorithms


For further information, see the section “The algorithm Environment” in the documentation of the algorithms package. But I want to stress again that (except for the float environment) the algorithmicx package is superior to algorithms.

• can you make a \listofalgorithms with it? i was checking out algorithmic but it doesn't seem to have a \function tag which is pretty disappointing
Aug 9, 2010 at 7:43
• @bada: This is not the algorithmic package, it’s algorithmicx, note the trailing x. It does have a \Function macro. For a list of algorithms, see updated answer. Aug 9, 2010 at 8:05
• @Naji Just remove the \DNA macros or define it appropriately. Apr 18, 2013 at 1:19
• @Naji Erm, that wasn’t what I said. But apparently your problem stems from something else: you need to load the fontspec package, load a font, and use XeLaTeX for compilation. Sorry, my answer should have mentioned that. See the update. Apr 19, 2013 at 7:14
• Hi @KonradRudolph would you be willing to recompose your example to be a TeX compatible minimal working example, with a list of the necessary packages that need to be separately downloaded and installed in your TeX repository. Right now it requires a lot of tweaking and extant know how to be able to get your example to work. Additionally, discussing the OP's question about compatibility with other packages would be useful. Thanks! May 24, 2015 at 20:19

This is a modification of @KonradRudolph's answer, but with a minimal working example along with a list of the necessarily installed packages that did not come with the basic LaTeX build. In addition I include a brief discussion of the TeX distribution's path, how to find it, and how to add a package to it in case you don't already know how. Hopefully this is a good extraction from the original that allows people who are newer to writing with TeX the ability to use this package more directly, jumping over some of the hurdles that were left unscaled in the original answer.

First, you will need to install algorithmicx in the path that your TeX distribution can read.

A good way to learn whether about the TeX distribution's paths covers a set of files is to walk through this tutorial by @kieran Healy, which describes the steps needed to make a font available to TeX, which is a much more arduous process (since you need to deal with font metric files, and have the license to the font in question, among other things).

Most importantly for the purposes here is the command

kpsexpand '$TEXMFLOCAL'  Which should print the local directory in your distribution's path to the console. If it doesn't work, try DEST=kpsexpand '$TEXMFLOCAL'&& echo $DEST  And if that doesn't work, I'd suggest reinstalling TeXLive if you are on OS X and I have no clue what to do if you aren't. Then, once you've identified your distribution's path and have downloaded the relevant CTAN files, create a new document and paste the following into it. \documentclass{article} \usepackage{algpseudocode,algorithm,algorithmicx} \newcommand*\DNA{\textsc{dna}} \newcommand*\Let[2]{\State #1$\gets$#2} \algrenewcommand\algorithmicrequire{\textbf{Precondition:}} \algrenewcommand\algorithmicensure{\textbf{Postcondition:}} \begin{document} \begin{algorithm} \caption{Counting mismatches between two packed \DNA{} strings \label{alg:packed-dna-hamming}} \begin{algorithmic}[1] \Require{$x$and$y$are packed \DNA{} strings of equal length$n$} \Statex \Function{Distance}{$x, y$} \Let{$z$}{$x \oplus y$} \Comment{$\oplus$: bitwise exclusive-or} \Let{$\delta$}{$0$} \For{$i \gets 1 \textrm{ to } n$} \If{$z_i \neq 0$} \Let{$\delta$}{$\delta + 1$} \EndIf \EndFor \State \Return{$\delta\$}
\EndFunction
\end{algorithmic}
\end{algorithm}

\end{document}


Once you run the command it should produce a PDF that looks like the following image:

Note that it is slightly different than the original that Konrad posted, as the nice sans-serif numbers and fontspec are unnecessary parts for someone just interested in getting algorithms to render properly.

NB: You should embed the algorithmic environment inside the algorithm environment, so that it is treated as a single floating entity and will not be broken across multiple pages.

• Got rid of the comments, good call. I'll leave the DNA bits, as using smallcaps for acronyms is generally a good practice to encourage even if just in examples. But the point is well taken. Grazie, @Austin. May 31, 2016 at 6:13

The FAQ page at https://www.texfaq.org/FAQ-algorithms provides an overview of the various packages for typesetting pseudocode in LaTeX.

• Dead link, and embedded search engine couldn't find the (possible) new page.
– kebs
Jun 26, 2020 at 9:56

If you like the typesetting used in the Introduction to Algorithm, here is the page for clrscode, which includes package for both the second and third edition.

I've been using listings package with mathescape=true option and a language definition to make it highlight my keywords. Something like that:

\lstdefinelanguage{alg}{
morekeywords={def,if,then,else,while,do,assert,end}
}