listenvironments — 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 similar 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. (There have been reports of difficulty defining new commands to fit with the package; unfortunately, the author is not available to comment.) 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.
This answer last edited: 2011-10-12
Go to previous question, or next question
Go to FAQ home.
URL for this question: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=algorithms
Comments, suggestions, or error reports? - see “how to improve the FAQ”.
This is FAQ version 3.28, released on 2014-06-10.