\usepackage{algorithm} for the numbered floating wrapper and \usepackage{algpseudocode} for the algorithmic environment and commands such as \State, \If, \For, \While, \Procedure, \Function, \Return, and \Comment. For asymptotic complexity notation, graphs, and other computer-science symbols, see Algorithms, complexity, and CS notation.
Complete working example
Pass[1] to \begin{algorithmic}[1] to number every line ([2] numbers every second line; omit it for unnumbered lines). Place \label after \caption inside the algorithm float so \ref resolves to the algorithm number.
pseudocode-basic.tex
Ready to use this syntax? Continue in the browser editor when you want to adapt the example in a real project.Open LaTeX Cloud Studio
algorithm vs algorithmic vs algorithmicx vs algpseudocode
These four packages have overlapping names but distinct roles:
Commands in algpseudocode
This example combines
\Function, \Call, \While, \If/\ElsIf/\Else, and \Comment:
pseudocode-procedure.tex
Alternative: algorithm2e
algorithm2e is a self-contained alternative that provides its own algorithm float, braced block syntax (\For{cond}{body}), and semicolon-terminated lines (\;). Use it when a publisher template asks for it or when you want vertical block lines (vlined) out of the box. Do not load algorithm2e together with algorithm (both define the algorithm environment and collide), and do not mix it with algpseudocode in the same document—choose one pseudocode package family.
algorithm2e-example.tex
Common mistakes
- Mixing
algorithmicandalgpseudocode:\STATEbelongs toalgorithmic;\Statebelongs toalgpseudocode. Load only one package. - Loading both
algorithmandalgorithm2e: both define\begin{algorithm}, causing a redefinition error. - Using
\captionwithoutalgorithm:\begin{algorithmic}alone is not a float. Wrap it in\begin{algorithm}...\end{algorithm}to add a caption and label. - Forgetting
\;inalgorithm2e: lines insidealgorithm2eneed\;(or\tcp{...}for comments) to end each statement cleanly. - Typesetting real source code in pseudocode: for actual programming languages with syntax highlighting, use Code listings and minted.
Related guides
Try it in the editor
algorithm, algpseudocode, and algorithm2e are installed in LaTeX Cloud Studio’s TeX Live 2026 environment. Start from the Basic Article template in LaTeX Cloud Studio and replace main.tex with the complete example.