LaTeX Guide

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)

KeywordUse
\StateSingle statement
\If{} ... \Else ... \EndIfConditional
\ElsIf{}Else-if branch
\While{} ... \EndWhileWhile loop
\For{} ... \EndForFor loop
\ForAll{} ... \EndForFor-each loop
\Procedure{}{} ... \EndProcedureFunction definition
\Function{}{} ... \EndFunctionFunction with return
\ReturnReturn 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. \If needs \EndIf, \While needs \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.
Typst pseudocode is just code blocks
#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 free

Frequently Asked Questions

How do I write pseudocode in LaTeX?

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.

What's the difference between algpseudocode and algorithm2e?

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.

How do I add line numbers to my algorithm?

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}.

How do I write if/else in LaTeX algorithms?

\If{condition} body \Else body \EndIf. For else-if chains: \If{...} ... \ElsIf{...} ... \Else ... \EndIf. Don't forget \EndIf — algpseudocode requires explicit closing keywords.

How do I write a while or for loop in LaTeX algorithm?

\While{condition} body \EndWhile. \For{i = 1, 2, ..., n} body \EndFor. \ForAll{x in collection} body \EndFor. The closing keywords are required.

How do I make algorithm comments?

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.

How do I caption and reference an algorithm?

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.

How do I make an algorithm float to a specific position?

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.

Why does my algorithm get cut off?

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.

More LaTeX guides