Let
X
Journal

How to Write Algorithms & Pseudocode in LaTeX

SPECIMEN IDLETX-SPEC-LATE
DATE RECORDEDMay 31, 2026
READING COMPLEXITY2 min read
TAG INDEX
latexalgorithmspseudocodetutorial
Document Abstract

To typeset pseudocode in LaTeX, use the algorithm float with algpseudocode (algorithmicx) or the algorithm2e package. Copy-paste templates for both inside.

To typeset pseudocode in LaTeX, use the algorithm float for the caption and one of two body packages: algpseudocode (from algorithmicx) or algorithm2e. The algorithm float gives your pseudocode a number and caption like a figure; the body package provides the keywords (if, while, for) with proper indentation. Pick one body package — they conflict if both are loaded.

1. The algpseudocode approach (algorithmicx)

\usepackage{algorithm}
\usepackage{algpseudocode}
...
\begin{algorithm}
\caption{Binary Search}
\label{alg:bsearch}
\begin{algorithmic}[1]   % [1] = number every line
  \Require sorted array $A$, target $x$
  \State $lo \gets 1$;\ $hi \gets n$
  \While{$lo \le 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$
\end{algorithmic}
\end{algorithm}

Reference it as Algorithm~\ref{alg:bsearch}. The [1] option numbers every line so reviewers can cite "line 7".

2. The algorithm2e approach

\usepackage[ruled,vlined]{algorithm2e}
...
\begin{algorithm}
\caption{Binary Search}
\KwIn{sorted array $A$, target $x$}
\KwOut{index of $x$ or $-1$}
$lo \gets 1$\;
$hi \gets n$\;
\While{$lo \le hi$}{
  $mid \gets \lfloor (lo+hi)/2 \rfloor$\;
  \uIf{$A[mid] = x$}{\Return $mid$\;}
  \uElseIf{$A[mid] < x$}{$lo \gets mid+1$\;}
  \Else{$hi \gets mid-1$\;}
}
\Return $-1$\;
\end{algorithm}

algorithm2e is more automated — line numbering and comments are built in.

3. Which to choose

| | algpseudocode | algorithm2e | |---|---|---| | Style | Explicit \State commands | Compact, automated | | Line numbers | [1] option | On by default | | Statement end | None needed | \; per line | | Don't combine with | algorithm2e | algpseudocode |

4. Math inside algorithms

All math mode works in pseudocode — use $...$ for variables and operators like \gets, \lfloor, \rfloor. Keep symbols consistent with your paper's notation.

5. The footnote caveat

Because algorithm is a float, \footnote fails inside it. Use \footnotemark in the algorithm and \footnotetext right after — see Footnotes & Margin Notes. For where algorithms sit in a paper, see How to Write a Research Paper.

→ Typeset and preview algorithms instantly in LetX.


Written by Shihab Shahriar Antor — AI Engineer & Founder of Shahriar Labs, maker of LetX.

Frequently Asked Questions

Which package should I use for algorithms: algpseudocode or algorithm2e?

Both are standard; pick one and don't load both, since they conflict. algorithmicx (which provides algpseudocode) gives you explicit commands like \State, \If, \While and pairs with the algorithm float for captioning — it's flexible and widely used in CS papers. algorithm2e is more automated, with a compact syntax and built-in line numbering and comments. Many IEEE and ACM templates work cleanly with either; check whether your venue's template already loads one.

How do I add line numbers to a LaTeX algorithm?

With algpseudocode, wrap the body in \begin{algorithmic}[1] — the optional [1] turns on numbering every line; use [2] to number every second line. With algorithm2e, line numbering is on by default and controlled by options to the package or environment. Line numbers let reviewers reference 'line 7' precisely, which is why most published algorithms include them.

Why can't I use \footnote inside an algorithm environment?

The algorithm environment is a float, like figure and table, and LaTeX does not process \footnote inside floats. The workaround is to use \footnotemark inside the algorithm and place the matching \footnotetext immediately after the float, which produces the footnote at the bottom of the page. Alternatively, put the explanatory note in the algorithm's caption or in the surrounding text.