Writing an algorithm in pseudocode in LaTeX

Help centre › LaTeX › Equations · Packages: algorithm, algpseudocode

Load \usepackage{algorithm} and \usepackage{algpseudocode}, then write the steps inside \begin{algorithm} \caption{…} \begin{algorithmic}[1] … \end{algorithmic} \end{algorithm} with \State, \If{…}/\EndIf, \For{…}/\EndFor and \While{…}/\EndWhile. The [1] numbers every line, and \label after \caption lets you refer to "Algorithm 1".

A complete example

A whole document: paste it into an empty project and compile it.

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Euclid's algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Require integers $a \ge 0$, $b \ge 0$
\Ensure the greatest common divisor of $a$ and $b$
\Function{Euclid}{$a, b$}
  \While{$b \neq 0$}
    \State $r \gets a \bmod b$
    \State $a \gets b$
    \State $b \gets r$ \Comment{swap and reduce}
  \EndWhile
  \State \Return $a$
\EndFunction
\end{algorithmic}
\end{algorithm}
Algorithm~\ref{alg:euclid} stops when $b$ reaches zero.
\end{document}

Two packages, two jobs

algorithm provides the floating box with its caption, "Algorithm 1", and a list of algorithms (\listofalgorithms). algpseudocode (part of algorithmicx) provides the pseudocode commands inside algorithmic. You can use algorithmic without the float when the pseudocode should stay exactly where it is written or run over a page break.

The commands

  • \State a statement; \Statex an unnumbered line.
  • \If{cond} … \ElsIf{cond} … \Else … \EndIf.
  • \For{cond} … \EndFor, \ForAll{cond} … \EndFor, \While{cond} … \EndWhile, \Repeat … \Until{cond}, \Loop … \EndLoop.
  • \Procedure{Name}{args} … \EndProcedure, \Function{Name}{args} … \EndFunction, \Call{Name}{args}, \Return.
  • \Require and \Ensure for input and output, \Comment{text} for a right-aligned comment.

Input/Output instead of Require/Ensure

Renaming the keywords changes them everywhere. \algrenewcommand works on any keyword; the noend option hides the "end if" and "end for" lines.

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}
\begin{document}
\begin{algorithm}
\caption{Linear search}\label{alg:search}
\begin{algorithmic}[1]
\Require a list $L$ of $n$ items, a value $v$
\Ensure the position of $v$ in $L$, or $0$
\For{$i \gets 1$ \textbf{to} $n$}
  \If{$L_i = v$}
    \State \Return $i$ \label{line:found}
  \EndIf
\EndFor
\State \Return $0$
\end{algorithmic}
\end{algorithm}
Line~\ref{line:found} of Algorithm~\ref{alg:search} returns early.
\end{document}

Referring to lines

With line numbering on ([1], or [5] to number every fifth line), a \label on a line records its number, so line~\ref{line:found} prints it, as in the example above.

Other packages

algorithm2e is a separate package with its own syntax (\eIf, \KwIn, ; at the end of each line). Choose one family per document: algorithm2e defines its own algorithm environment and clashes with the algorithm package. The older algorithmic package uses uppercase commands (\STATE, \IF); do not load it together with algpseudocode.

Common mistakes

In journal templates

Errors you may meet

  • Undefined control sequence.LaTeX met a command it does not know: usually a typo, or a command whose package is not loaded. The log's l. line ends with the culprit.
  • LaTeX Error: Environment <name> undefined\begin{…} names an environment no loaded package defines: load the package (amsmath for align), fix the spelling, or define it with \newtheorem.
  • Missing $ inserted.A maths-only character or command (_ ^ \alpha) appeared in text, or text appeared in maths. Wrap the maths in $ … $ or escape the character.

In Manulyx

Typing \begin{ suggests environments and writes the rest for you, and every compile runs the extra passes that turn "Algorithm ??" into its number.

Write in Manulyx for free · Journal templates

Related

The examples on this page were compiled with pdfLaTeX to check that they work as shown. LaTeX quick reference · LaTeX errors explained