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 mintedMinimal Setup
- Code
- Rendered output
pseudocode-setup.tex
What Each Package Does
| Package | Purpose |
|---|---|
algorithm | Creates the numbered floating environment with caption support |
algorithmicx | Provides the pseudocode framework |
algpseudocode | Adds common pseudocode commands and style |
Common Pseudocode Commands
- Code
- Rendered output
pseudocode-commands.tex
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
algorithmwhen you want captions, numbering, and float placement - Use
algpseudocodewhen you want readable pseudocode commands - Use both together for the most common LaTeX pseudocode workflow
Alternatives
algorithm2egives you a different all-in-one style and syntaxlistingsormintedare 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
algorithmenvironment - Formatting looks like code rather than pseudocode: use
algpseudocode, notlistings
