# LaTeX/Algorithms

* LaTeX
* Getting Started
* Introduction
* Installation
* Installing Extra Packages
* Basics
* How to get help
* Common Elements
* Document Structure
* Text Formatting
* Paragraph Formatting
* Colors
* Fonts
* List Structures
* Special Characters
* Internationalization
* Rotations
* Tables
* Title creation
* Page Layout
* Customizing Page Headers and Footers‎
* Importing Graphics
* Floats, Figures and Captions
* Footnotes and Margin Notes
* Hyperlinks
* Labels and Cross-referencing
* Initials
* Mechanics
* Errors and Warnings
* Lengths
* Counters
* Boxes
* Rules and Struts
* Technical Text
* Mathematics
* Advanced Mathematics
* Theorems
* Chemical Graphics
* Algorithms
* Source Code Listings
* Linguistics
* Special Pages
* Indexing
* Glossary
* Bibliography Management
* More Bibliographies
* Special Documents
* Scientific Reports (Bachelor Report, Master Thesis, Dissertation)
* Letters
* Presentations
* Teacher's Corner
* Curriculum Vitae
* Academic Journals (MLA, APA, etc.)
* Creating Graphics
* Introducing Procedural Graphics
* MetaPost
* Picture
* PGF/TikZ
* PSTricks
* Xy-pic
* Creating 3D graphics
* Programming
* Macros
* Plain TeX
* Creating Packages
* Creating Package Documentation
* Themes
* Miscellaneous
* Modular Documents
* Collaborative Writing of LaTeX Documents
* Export To Other Formats
* Help and Recommendations
* FAQ
* Tips and Tricks
* Appendices
* Authors
* Links
* Package Reference
* Sample LaTeX documents
* Index
* Command Glossary
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 algorithmic 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 algorithm2e package

The algorithm2e package (first released 1995, latest updated July 2017 according to the v5.2 manual) allows typesetting algorithms with a lot of customization. Like algorithmic, this package is also not compatible with Revtex-4.1.
Unlike algorithmic, algorithm2e 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 \begin{algorithm} and \end{algorithm} 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 algorithmicx package

The algorithmicx package provides a number of popular constructs for algorithm designs. Put \usepackage{algpseudocode} in the preamble to use the algorithmic environment to write algorithm pseudocode (\begin{algorithmic}...\end{algorithmic}). You might want to use the algorithm environment (\usepackage{algorithm}) to wrap your algorithmic code in an algorithm environment (\begin{algorithm}...\end{algorithm}) to produce a floating environment with numbered algorithms.
The command \begin{algorithmic} 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. \begin{algorithmic}[5] will enter the algorithmic environment and number every fifth line.
Below is an example of typesetting a basic algorithm using the algorithmicx package (remember to add the \usepackage{algpseudocode} 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 \ElsIf clauses as required. Note that it is \ElsIf and not \ElseIf.
Loops:
Pre- and postcondition:
Functions
This command will usually be used in conjunction with a \State command as follows:
Comments:
Note to users who switched from the old algorithmic package: comments may be placed everywhere in the source; there are no limitations as in the old algorithmic package.
The algorithmicx 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 \<start> and \<end> which have no parameters. The text displayed by them is \textbf{<start>} and \textbf{<end>}.
With \algblockdefx 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 #n.
Example:
More advanced customization and other constructions are described in the algorithmicx manual: http://mirror.ctan.org/macros/latex/contrib/algorithmicx/algorithmicx.pdf

### Typesetting using the program package

The program package provides macros for typesetting algorithms. Each line is set in math mode, so all the indentation and spacing is done automatically. The notation |variable_name| can be used within normal text, maths expressions or programs to indicate a variable name. Use \origbar to get a normal | symbol in a program. The commands \A, \B, \P, \Q, \R, \S, \T and \Z typeset the corresponding bold letter with the next object as a subscript (eg \S1 typesets {\bf S$_1$} etc). Primes work normally, eg \S‘‘.
Below is an example of typesetting a basic algorithm using the program package (remember to add the \usepackage{program} statement to your document preamble):
The commands \( and \) 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: \set{x \in N | x > 0}.
Lines can be numbered by setting \NumberProgramstrue and numbering turned off with \NumberProgramsfalse
Package page
Package documentation

## The algorithm environment

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

### Algorithm numbering

The default numbering system for the algorithm 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 algorithm 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

## References

* ↑ http://tex.stackexchange.com/questions/70181/revtex4-1-and-algorithm2e-indentation-clash
* The official manual for the algorithms package, Rogério Brito (2009), http://mirrors.ctan.org/macros/latex/contrib/algorithms/algorithms.pdf