[Tex/LaTex] a good package for displaying algorithms


I found this page with some examples:

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.

Best Answer

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.

algpseudocode example

This was produced by the following code:

  \caption{Counting mismatches between two packed \DNA{} strings
    \Require{$x$ and $y$ are packed \DNA{} strings of equal length $n$}
    \Function{Distance}{$x, y$}
      \Let{$z$}{$x \oplus y$} \Comment{$\oplus$: bitwise exclusive-or}
      \For{$i \gets 1 \textrm{ to } n$}
        \If{$z_i \neq 0$}
          \Let{$\delta$}{$\delta + 1$}
      \State \Return{$\delta$}

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

\setmainfont{Hoefler Text}

\newcommand*\Let[2]{\State #1 $\gets$ #2}

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


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


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.