LaTeX/Algorithms

LaTeX has several packages for typesetting algorithms in form of "pseudocode". They provide stylistic enhancements over a uniform style (i.e., all in typewriter font) so that constructs such as loops or conditionals are visually separated from other text. The pseudocode is usually put in an algorithm environment. For typesetting real code, written in a real programming language, consider the listings package described in Source Code Listings.

Typesetting
There are four notable packages algorithmic, algorithm2e, algorithmicx, and program,

Typesetting using the algorithmic package
The algorithmic package uses a different set of commands than the algorithmicx package. This is not compatible with revtex4-1. Basic commands are: Complete documentation is listed at. Most commands are similar to the algorithmicx equivalents, but with different capitalization. The package algorithms bundle at the ctan repository, dated 2009-08-24, describes both the algorithmic environment (for typesetting algorithms) and the algorithm floating wrapper (see below) which is designed to wrap around the algorithmic environment.

The <tt>algorithmic</tt> package is suggested for IEEE journals as it is a part of their default style sheet.

How to rename require/ensure to input/output:

Typesetting using the <tt>algorithm2e</tt> package
The <tt>algorithm2e</tt> package (first released 1995, latest updated July 2017 according to the v5.2 manual) allows typesetting algorithms with a lot of customization. Like <tt>algorithmic</tt>, this package is also not compatible with Revtex-4.1.

Unlike <tt>algorithmic</tt>, <tt>algorithm2e</tt> provides a relatively huge number of customization options to the algorithm suiting to the needs of various users. The CTAN-manual provides a comprehensible list of examples and full set of controls.

Typically, the usage between <tt>\begin{algorithm}</tt> and <tt>\end{algorithm}</tt> would be 1. Declaring a set of keywords(to typeset as functions/operators), layout controls, caption, title, header text (which appears before the algorithm's main steps e.g.: Input,Output) 2. Writing the main steps of the algorithm, with each step ending with a \; This may be taken in analogy with writing a latex-preamble before we start the actual document.

The package is loaded like and a simple example, taken from the v4.01 manual, is which produces



More details are in the manual hosted on the ctan website.

Typesetting using the <tt>algorithmicx</tt> package
The <tt>algorithmicx</tt> package provides a number of popular constructs for algorithm designs. Put <tt>\usepackage{algpseudocode}</tt> in the preamble to use the algorithmic environment to write algorithm pseudocode (<tt>\begin{algorithmic}...\end{algorithmic}</tt>). You might want to use the algorithm environment (<tt>\usepackage{algorithm}</tt>) to wrap your algorithmic code in an algorithm environment (<tt>\begin{algorithm}...\end{algorithm}</tt>) to produce a floating environment with numbered algorithms.

The command <tt>\begin{algorithmic}</tt> can be given the optional argument of a positive integer, which if given will cause line numbering to occur at multiples of that integer. E.g. <tt>\begin{algorithmic}[5]</tt> will enter the algorithmic environment and number every fifth line.

Below is an example of typesetting a basic algorithm using the <tt>algorithmicx</tt> package (remember to add the <tt>\usepackage{algpseudocode}</tt> statement to your document preamble):

The LaTeX source can be written to a format familiar to programmers so that it is easy to read. This will not, however, affect the final layout in the document.



Basic commands have the following syntax:

Statement (\State causes a new line, can also be used in front of other commands)

Three forms of if-statements: The third form accepts as many <tt>\ElsIf{}</tt> clauses as required. Note that it is <tt>\ElsIf</tt> and not <tt>\ElseIf</tt>.

Loops:

Pre- and postcondition:

Functions

This command will usually be used in conjunction with a <tt>\State</tt> command as follows:

Comments:

Note to users who switched from the old <tt>algorithmic</tt> package: comments may be placed everywhere in the source; there are no limitations as in the old <tt>algorithmic</tt> package.

The <tt>algorithmicx</tt> package allows you to define your own environments.

To define blocks beginning with a starting command and ending with an ending command, use This defines two commands <tt>\ </tt> and <tt>\ </tt> which have no parameters. The text displayed by them is <tt>\textbf{ }</tt> and <tt>\textbf{ }</tt>.

With <tt>\algblockdefx</tt> you can give the text to be output by the starting and ending command and the number of parameters for these commands. In the text the n-th parameter is referenced by <tt>#n</tt>.

Example:

More advanced customization and other constructions are described in the <tt>algorithmicx</tt> manual: http://mirror.ctan.org/macros/latex/contrib/algorithmicx/algorithmicx.pdf

Typesetting using the <tt>program</tt> package
The <tt>program</tt> package provides macros for typesetting algorithms. Each line is set in math mode, so all the indentation and spacing is done automatically. The notation <tt>|variable_name|</tt> can be used within normal text, maths expressions or programs to indicate a variable name. Use <tt>\origbar</tt> to get a normal <tt>|</tt> symbol in a program. The commands <tt>\A</tt>, <tt>\B</tt>, <tt>\P</tt>, <tt>\Q</tt>, <tt>\R</tt>, <tt>\S</tt>, <tt>\T</tt> and <tt>\Z </tt>typeset the corresponding bold letter with the next object as a subscript (eg <tt>\S1</tt> typesets <tt>{\bf S$_1$}</tt> etc). Primes work normally, eg <tt>\S‘‘</tt>.

Below is an example of typesetting a basic algorithm using the <tt>program</tt> package (remember to add the <tt>\usepackage{program}</tt> statement to your document preamble):



The commands <tt>\(</tt> and <tt>\)</tt> are redefined to typeset an algorithm in a minipage, so an algorithm can appear as a single box in a formula. For example, to state that a particular action system is equivalent to a WHILE loop you can write:

Dijkstra conditionals and loops:

Loops with multiple exits:

A Reverse Engineering Example.

Here's the original program:

And here's the transformed and corrected version:

The package also provides a macro for typesetting a set like this: <tt>\set{x \in N | x > 0}</tt>.

Lines can be numbered by setting <tt>\NumberProgramstrue</tt> and numbering turned off with <tt>\NumberProgramsfalse</tt>

Package page

Package documentation

The <tt>algorithm</tt> environment
It is often useful for the algorithm produced by <tt>algorithmic</tt> to be "floated" to the optimal point in the document to avoid it being split across pages. The <tt>algorithm</tt> environment provides this and a few other useful features. Include it by adding the <tt>\usepackage{algorithm}</tt> to your document's preamble. It is entered into by

Algorithm numbering
The default numbering system for the <tt>algorithm</tt> package is to number algorithms sequentially. This is often not desirable, particularly in large documents where numbering according to chapter is more appropriate. The numbering of algorithms can be influenced by providing the name of the document component within which numbering should be recommenced. The legal values for this option are: part, chapter, section, subsection, subsubsection or nothing (default). For example:

List of algorithms
When you use figures or tables, you can add a list of them close to the table of contents; the <tt>algorithm</tt> package provides a similar command. Just put anywhere in the document, and LaTeX will print a list of the "algorithm" environments in the document with the corresponding page and the caption.

An example from the manual
This is an example taken from the manual (official manual, p.14)


 * The official manual is located at: http://mirrors.ctan.org/macros/latex/contrib/algorithms/algorithms.pdf