Technical Writing
Algorithms
Typesetting pseudocode and structured algorithms in LaTeX using algorithmic, algorithmicx, and algorithm2e.
LaTeX has several packages for typesetting algorithms in the form of "pseudocode". They provide stylistic enhancements (bold keywords, indented blocks) so that constructs like loops or conditionals are visually separated from normal text.
Notable Packages
There are three main packages used to typeset algorithms:
algorithmicx/algpseudocode: The modern, highly customisable standard.algorithm2e: A feature-rich package that uses a declarative keyword style.algorithmic: The legacy package (often required by older IEEE journals).
Using algorithmicx (algpseudocode)
To write pseudocode, load the package in your preamble:
\usepackage{algpseudocode}
\usepackage{algorithm} % Wraps the pseudocode in a floating numbered wrapper
Basic Syntax
\begin{algorithm}
\caption{Euclid's algorithm}
\begin{algorithmic}[1] % [1] enables line numbering on every line
\Procedure{Euclid}{$a,b$}
\State $r \gets a \bmod b$
\While{$r \neq 0$}
\State $a \gets b$
\State $b \gets r$
\State $r \gets a \bmod b$
\EndWhile
\State \textbf{return} $b$
\EndProcedure
\end{algorithmic}
\end{algorithm}
Common Commands
| Command | Description |
|---|---|
\State | A statement |
\If{cond} ... \Else ... \EndIf | Conditional |
\While{cond} ... \EndWhile | While loop |
\For{cond} ... \EndFor | For loop |
\Repeat ... \Until{cond} | Repeat-until loop |
\Procedure{name}{args} ... \EndProcedure | Procedure |
\Function{name}{args} ... \EndFunction | Function |
\Return | Return statement |
\Require | Input declaration |
\Ensure | Output declaration |
Line Numbering Options
\begin{algorithmic}[1]— Number every line\begin{algorithmic}[5]— Number every 5th line\begin{algorithmic}[0]— No line numbers (default)
Using algorithm2e
Load algorithm2e in your preamble:
\usepackage[ruled,vlined]{algorithm2e}
Inside the document, write commands terminated with a semicolon \;:
\begin{algorithm}[H]
\KwData{this text}
\KwResult{how to write algorithm with LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
}{
go back to the beginning\;
}
}
\caption{How to write algorithms}
\end{algorithm}
algorithm2e Options
| Option | Description |
|---|---|
ruled | Rules around algorithm |
vlined | Vertical lines for blocks |
boxed | Box around algorithm |
linesnumbered | Line numbering |
algo2e | Use \SetAlgo* commands |
algorithm2e Placement
Use [H] for exact placement (requires float package), or [htbp] for flexible placement:
\begin{algorithm}[H] % Exact placement
\begin{algorithm}[htbp] % Flexible placement
Comparison: algorithmicx vs algorithm2e
| Feature | algorithmicx | algorithm2e |
|---|---|---|
| Syntax style | Environment-based | Command-based (\;) |
| Customization | Highly flexible via algpseudocode | Predefined styles |
| Block lines | Manual via algpseudocode | vlined option |
| Placement | Via algorithm float | Built-in float support |
| Learning curve | Moderate | Easy |
Choosing a Package
- New documents: Use
algorithmicxwithalgpseudocode— most flexible and widely supported. - Quick prototypes: Use
algorithm2e— simpler syntax for quick pseudocode. - IEEE journals: Check if they require the legacy
algorithmicpackage.