### Typesetting pseudocode in LaTeX

There is no consensus on the ‘right’ way to typeset pseudocode. Consequently, there are a variety of LaTeX packages to choose from for producing æsthetically pleasing pseudocode listings.

Pseudocode differs from actual program listings in that it lacks strict syntax and semantics. Also, because pseudocode is supposed to be a clear expression of an algorithm it may need to incorporate mathematical notation, figures, tables, and other LaTeX features that do not appear in conventional programming languages. Typesetting program listings is described elsewhere.

You can certainly create your own environment for typesetting pseudocode using, for example, the `tabbing` or `list` environments — it’s not difficult, but it may prove boring. So it’s worth trying the following packages, all designed specifically for typesetting pseudocode.

The algorithms bundle (which contains packages algorithm and algorithmic, both of which are needed for ordinary use) has a simple interface and produces fairly nice output. It provides primitives for statements, which can contain arbitrary LaTeX commands, comments, and a set of iterative and conditional constructs. These primitives can easily be redefined to produce different text in the output. However, there is no support for adding new primitives. Typesetting the pseudocode itself is performed in algorithmic; the algorithms package uses the facilities of the float package to number algorithms sequentially, enable algorithms to float like figures or tables, and support including a List of Algorithms in a document’s front matter.

Packages in the algorithmicx bundle are similiar both in concept and output form to algorithmic but additionally provide support for adding new keywords and altering the formatting. It provides the algpseudocode package which is (almost) a drop-in replacement for algorithmic. Another package in the bundle, algpascal, uses Pascal-like keywords, indents differently from algpseudocode, and puts command arguments in maths mode instead of text mode. There is no floating environment but algorithmicx, like algorithmic, is compatible with the algorithm package.

The alg package, like algorithms, offers a floating algorithm environment with all of the ensuing niceties. alg, however, can caption its floats in a variety of (natural) languages. In addition, alg unlike algorithms, makes it easy to add new constructs.

The newalg package has a somewhat similar interface to algorithms, but its output is designed to mimic the rather pleasant typesetting used in the book “Introduction to Algorithms” by Corman, Leiserson, Rivest and Stein. Unfortunately, newalg does not support a floating environment or any customisation of the output.

“Bona fide” use of the style of “Introduction to Algorithms” may be achieved with Cormen’s own clrscode: this is the package as used in the second edition of the book.

Similarly, the style of “Combinatorial Algorithms: Generation, Enumeration and Search” is supported by the pseudocode package, written by the authors of the book. It has the common ‘Pascal-like’ style, and has some interesting constructs for what one thinks of as Pascal blocks.

The algorithm2e is of very long standing, and is widely used and recommended. It loads the float package to provide the option of floating algorithm descriptions, but you can always use the “`H`” option of float to have the algorithm appear “where you write it”.

The usage of the program package is a little different from that of the other packages. It typesets programs in maths mode instead of text mode; and linebreaks are significant. program lacks a floating environment but does number algorithms like alg and algorithms. Customisation and extension are not supported. Documentation of the program package (such as it is) appears in a file program.msg in the distribution.

None of the above are perfect. The factors that should influence your choice of package include the output style you prefer, how much you need to extend or modify the set of keywords, and whether you require algorithms to float like figures and tables.

`algorithm2e.sty`
  macros/latex/contrib/algorithm2e (gzipped tar, browse)
`algorithmicx bundle`
  macros/latex/contrib/algorithmicx (gzipped tar, browse)
`algorithms bundle`
  macros/latex/contrib/algorithms (gzipped tar, browse)
`alg.sty`
  macros/latex/contrib/alg (gzipped tar, browse)
`clrscode.sty`
  macros/latex/contrib/clrscode (gzipped tar, browse)
`float.sty`
  macros/latex/contrib/float (gzipped tar, browse)
`newalg.sty`
  macros/latex/contrib/newalg (gzipped tar, browse)
`program.sty`
  macros/latex/contrib/program (gzipped tar, browse)
`pseudocode.sty`
  macros/latex/contrib/pseudocode (gzipped tar, browse)

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=algorithms