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:

  1. algorithmicx / algpseudocode: The modern, highly customisable standard.
  2. algorithm2e: A feature-rich package that uses a declarative keyword style.
  3. 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

CommandDescription
\StateA statement
\If{cond} ... \Else ... \EndIfConditional
\While{cond} ... \EndWhileWhile loop
\For{cond} ... \EndForFor loop
\Repeat ... \Until{cond}Repeat-until loop
\Procedure{name}{args} ... \EndProcedureProcedure
\Function{name}{args} ... \EndFunctionFunction
\ReturnReturn statement
\RequireInput declaration
\EnsureOutput 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

OptionDescription
ruledRules around algorithm
vlinedVertical lines for blocks
boxedBox around algorithm
linesnumberedLine numbering
algo2eUse \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

Featurealgorithmicxalgorithm2e
Syntax styleEnvironment-basedCommand-based (\;)
CustomizationHighly flexible via algpseudocodePredefined styles
Block linesManual via algpseudocodevlined option
PlacementVia algorithm floatBuilt-in float support
Learning curveModerateEasy

Choosing a Package

  • New documents: Use algorithmicx with algpseudocode — most flexible and widely supported.
  • Quick prototypes: Use algorithm2e — simpler syntax for quick pseudocode.
  • IEEE journals: Check if they require the legacy algorithmic package.
Copyright © 2026