Write pseudocode in LaTeX (algorithm, algpseudocode)
Load algorithm and algpseudocode. The algorithm environment is a float with a caption, and the algorithmic environment inside it holds the statements, written with commands such as \State, \If, \For and \While.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{algorithm}
\usepackage{algpseudocode}Write an algorithm with a loop and a condition
Every simple statement starts with \State. Blocks have an opening and a closing command: \If{...} and \EndIf, \For{...} and \EndFor, \While{...} and \EndWhile. \Require and \Ensure state input and result. The [1] after \begin{algorithmic} numbers every line; [5] would number every fifth.
\begin{algorithm}
\caption{Largest element of a list}
\label{alg:max}
\begin{algorithmic}[1]
\Require a list $A$ with $n \ge 1$ elements
\Ensure the largest element of $A$
\State $m \gets A[1]$
\For{$i \gets 2$ \textbf{to} $n$}
\If{$A[i] > m$}
\State $m \gets A[i]$
\EndIf
\EndFor
\State \Return $m$
\end{algorithmic}
\end{algorithm}Use functions, comments and line references
\Function{name}{arguments} and \EndFunction enclose a function, and \Call{name}{arguments} prints a call of it. \Comment{...} puts a remark at the right edge of the line. A \label at the end of a statement lets \ref print its line number.
\begin{algorithm}
\caption{Euclidean algorithm}
\label{alg:gcd}
\begin{algorithmic}[1]
\Function{Gcd}{$a, b$}
\While{$b \neq 0$} \Comment{stop when the remainder is zero}
\State $r \gets a \bmod b$ \label{line:mod}
\State $a \gets b$
\State $b \gets r$
\EndWhile
\State \Return $a$
\EndFunction
\end{algorithmic}
\end{algorithm}
Line~\ref{line:mod} of Algorithm~\ref{alg:gcd} computes the remainder.
The call \Call{Gcd}{$12, 8$} returns 4.Leave out the end lines and rename keywords
The package option noend drops the lines "end if", "end for" and "end while"; you still write \EndIf and the other closing commands. \algrenewcommand replaces the text of a keyword, here "Require:" and "Ensure:" with "Input:" and "Output:".
Complete document:
\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}
\begin{document}
\begin{algorithm}
\caption{Sum of the even numbers up to $n$}
\begin{algorithmic}[1]
\Require a number $n$
\Ensure the sum $s$
\State $s \gets 0$
\For{$i \gets 1$ \textbf{to} $n$}
\If{$i$ is even}
\State $s \gets s + i$
\EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}Alternative: the algorithm2e package
algorithm2e uses one environment and its own syntax: every statement ends with \;, and blocks stand in braces, as in \While{condition}{body}. The options ruled, vlined and linesnumbered add rules above and below, vertical lines along the blocks and line numbers.
Complete document:
\documentclass{article}
\usepackage[ruled, vlined, linesnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}
\caption{Euclidean algorithm}
\KwIn{integers $a \ge b > 0$}
\KwOut{the greatest common divisor of $a$ and $b$}
\While{$b \neq 0$}{
$r \gets a \bmod b$\;
$a \gets b$\;
$b \gets r$\;
}
\Return{$a$}\;
\end{algorithm}
\end{document}Pitfalls
- Do not load
algorithm2etogether withalgorithm. Both define thealgorithmenvironment, and the second one stops with Command \vec already defined. - The older
algorithmicpackage writes its commands in capitals (\STATE,\IF,\ENDIF). Withalgpseudocodethese stop with Undefined control sequence; write\State,\Ifand\EndIf. - An
algorithmfloats like a figure.\begin{algorithm}[H]keeps it at its place in the text;algorithmloads thefloatpackage that providesH.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
algpseudocode - Package
algorithm2e - How-to Insert source code in LaTeX
- How-to Cross-references in LaTeX
- Error Command \vec already defined