LaTeX Algorithm & Pseudocode
Use algorithm for the float and caption, algpseudocode for the pseudocode body. \State, \If, \While, \For are the building blocks.
The standard pattern
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{algorithm}
\caption{Binary search}
\label{alg:binsearch}
\begin{algorithmic}[1]
\Procedure{BinarySearch}{$A, x$}
\State $lo \gets 1$
\State $hi \gets |A|$
\While{$lo \leq hi$}
\State $mid \gets \lfloor (lo + hi) / 2 \rfloor$
\If{$A[mid] = x$}
\State \Return $mid$
\ElsIf{$A[mid] < x$}
\State $lo \gets mid + 1$
\Else
\State $hi \gets mid - 1$
\EndIf
\EndWhile
\State \Return $-1$ \Comment{not found}
\EndProcedure
\end{algorithmic}
\end{algorithm}Common keywords (algpseudocode)
| Keyword | Use |
|---|---|
| \State | Single statement |
| \If{} ... \Else ... \EndIf | Conditional |
| \ElsIf{} | Else-if branch |
| \While{} ... \EndWhile | While loop |
| \For{} ... \EndFor | For loop |
| \ForAll{} ... \EndFor | For-each loop |
| \Procedure{}{} ... \EndProcedure | Function definition |
| \Function{}{} ... \EndFunction | Function with return |
| \Return | Return value |
| \Comment{} | Right-aligned comment |
Alternative: algorithm2e
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\begin{algorithm}
\caption{Binary search (algorithm2e)}
\KwIn{Sorted array $A$, target $x$}
\KwOut{Index of $x$ in $A$, or $-1$}
$lo \gets 1$\;
$hi \gets |A|$\;
\While{$lo \leq hi$}{
$mid \gets \lfloor (lo + hi) / 2 \rfloor$\;
\eIf{$A[mid] = x$}{
\Return $mid$\;
}{
\eIf{$A[mid] < x$}{$lo \gets mid + 1$\;}{$hi \gets mid - 1$\;}
}
}
\Return $-1$\;
\end{algorithm}algorithm2e uses block-style indentation with curly braces and explicit line endings (\;). Different aesthetic, same capabilities.
Common mistakes
- Loading both algorithmic and algpseudocode. They conflict — pick one. algpseudocode is the modern choice.
- Forgetting the closing keyword.
\Ifneeds\EndIf,\Whileneeds\EndWhile, etc. Easy to miss. - Mixing algpseudocode and algorithm2e syntax. They're different packages — pick one.
- Algorithm doesn't fit on a page. Use the algpseudocodex variant for page-breaking, or split into smaller procedures.
#figure(
caption: [Binary search]
)[
```
procedure BinarySearch(A, x):
lo ← 1; hi ← |A|
while lo ≤ hi:
mid ← ⌊(lo + hi) / 2⌋
if A[mid] = x: return mid
elif A[mid] < x: lo ← mid + 1
else: hi ← mid - 1
return -1
```
]Use real Unicode arrows and floor brackets, syntax-highlight via Typst's built-in raw blocks. Try TypeTeX free.
Try TypeTeX freeFrequently Asked Questions
Use the algorithm and algpseudocode packages: \usepackage{algorithm} \usepackage{algpseudocode}. Then \begin{algorithm} \caption{Title} \begin{algorithmic}[1] \State ... \end{algorithmic} \end{algorithm}. The [1] enables line numbering. \State, \If, \While, \For, \Procedure are the keywords.
Two competing packages. algpseudocode (part of algorithmicx) uses LaTeX-style commands (\State, \If, \Else). algorithm2e uses a different syntax with \KwIn, \KwOut, \eIf{condition}{then}{else}. Both are widely used; pick one and stick with it. algpseudocode is more common in CS theory papers.
Pass [1] as an argument to algorithmic: \begin{algorithmic}[1]. The number tells LaTeX to number every Nth line. [1] numbers all lines, [2] every other, etc. Reference specific lines with \State \label{ln:name} and \ref{ln:name}.
\If{condition} body \Else body \EndIf. For else-if chains: \If{...} ... \ElsIf{...} ... \Else ... \EndIf. Don't forget \EndIf — algpseudocode requires explicit closing keywords.
\While{condition} body \EndWhile. \For{i = 1, 2, ..., n} body \EndFor. \ForAll{x in collection} body \EndFor. The closing keywords are required.
Use \Comment{your comment}: \State $x \gets x + 1$ \Comment{Increment counter}. The comment appears right-aligned on the same line, in italic by default.
Inside the algorithm float: \caption{Algorithm name} \label{alg:name}. Reference with \ref{alg:name} or \autoref{alg:name}. Algorithms are numbered separately from figures and tables.
Pass position to the algorithm environment: \begin{algorithm}[t] for top, [b] for bottom, [h] for here, [H] (with the float package) for 'force here'. [tbp] is a good default — let LaTeX pick top, bottom, or own page.
Long algorithms don't break across pages by default. Use the algpseudocodex package (a fork of algpseudocode) which supports page-breaking, or restructure into shorter sub-procedures with \Procedure{} ... \EndProcedure.