Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I found this page with some examples:
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.

share|improve this question
1  
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?" :) –  Amir Rachum Aug 9 '10 at 7:28
    
Related: Print programs with its proper syntax –  Werner Jun 4 at 22:05
add comment

5 Answers 5

Personally, I favour algpseudocode from the algorithmicx (note the trailing x!) package. With a bit of setup, this can be harnessed to create beautiful pseudo codes. For an example of its usage, have a look at the “Algorithm” chapter of my bachelor thesis, e.g.:

algpseudocode example

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]{
    {\sf\footnotesize\addfontfeatures{Colour=888888,Numbers=Monospaced}#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.

share|improve this answer
    
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 –  bada Aug 9 '10 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. –  Konrad Rudolph Aug 9 '10 at 8:05
    
@KonradRudolph, I am trying to run your code, but I get: Undefined control sequence for 3rd line of algorithm block. I would be really happy if you help me figure it out. –  Naji Apr 17 '13 at 23:49
1  
@Naji Just remove the \DNA macros or define it appropriately. –  Konrad Rudolph Apr 18 '13 at 1:19
1  
@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. –  Konrad Rudolph Apr 19 '13 at 7:14
show 1 more comment

The FAQ page at http://www.tex.ac.uk/cgi-bin/texfaq2html?label=algorithms provides an overview of the various packages for typesetting pseudocode in LaTeX.

share|improve this answer
add comment

I've used Algorithm2e successfully in the past.

share|improve this answer
14  
Try turning this into an answer by providing some insight as to why it was successful/useful, by providing a comparison with other packages, etc. –  Marc van Dongen Nov 30 '12 at 8:04
add comment

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.

share|improve this answer
add comment

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}
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.