Skip to main content
If you want to write pseudocode in LaTeX, the most common setup is algorithm for the floating environment and algpseudocode from the algorithmicx family for the pseudocode commands.
Quick answer: load \usepackage{algorithm} and \usepackage{algpseudocode}, then write steps with commands such as \State, \If, \For, \While, and \Procedure.Related topics: Algorithms, complexity, and CS notation | Code listings and minted

Minimal Setup

pseudocode-setup.tex
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Linear Search}
\begin{algorithmic}[1]
\For{$i = 1$ \textbf{to} $n$}
    \If{$A[i] = x$}
        \State \Return $i$
    \EndIf
\EndFor
\State \Return \textsc{not found}
\end{algorithmic}
\end{algorithm}

\end{document}

What Each Package Does

PackagePurpose
algorithmCreates the numbered floating environment with caption support
algorithmicxProvides the pseudocode framework
algpseudocodeAdds common pseudocode commands and style

Common Pseudocode Commands

pseudocode-commands.tex
\begin{algorithmic}[1]
\Procedure{BinarySearch}{$A, x$}
    \State $left \gets 1$
    \State $right \gets \Call{length}{A}$
    \While{$left \leq right$}
        \State $mid \gets \lfloor (left + right) / 2 \rfloor$
        \If{$A[mid] = x$}
            \State \Return $mid$
        \ElsIf{$A[mid] < x$}
            \State $left \gets mid + 1$
        \Else
            \State $right \gets mid - 1$
        \EndIf
    \EndWhile
    \State \Return \textsc{not found}
\EndProcedure
\end{algorithmic}

Numbered Lines and Procedures

The optional [1] after \begin{algorithmic} turns on line numbers. Use \Procedure and \Function when you want named blocks that look like textbook pseudocode.

algorithm vs algorithmicx

  • Use algorithm when you want captions, numbering, and float placement
  • Use algpseudocode when you want readable pseudocode commands
  • Use both together for the most common LaTeX pseudocode workflow

Alternatives

  • algorithm2e gives you a different all-in-one style and syntax
  • listings or minted are better when you want real source code, not pseudocode
  • Plain math environments can work for very short algorithm descriptions, but scale badly

Common Errors

  • Environment algorithm undefined: load \usepackage{algorithm}
  • Undefined control sequence \State: load \usepackage{algpseudocode}
  • Caption not showing: make sure the pseudocode is inside the algorithm environment
  • Formatting looks like code rather than pseudocode: use algpseudocode, not listings

When to Use This Page vs the Algorithms Guide

Use this page when you specifically need to format pseudocode. Use the broader algorithms guide when you also need complexity notation, graph notation, or other computer-science symbols.