algorithm for the numbered float and algpseudocode for commands such as \State, \If, \For, and \Procedure. Put an algorithmic block inside algorithm when you need both formatted steps and a caption.
Quick answer:
algorithm controls caption, numbering, and float placement; algpseudocode supplies the readable pseudocode syntax. The complete example below uses both and compiles with pdfLaTeX.Related topics: Algorithms, complexity, and CS notation | Code listings and mintedMinimal Working Pseudocode Example
The optional[1] after \begin{algorithmic} enables a line number on every line. Remove it when the algorithm should be unnumbered.
What Each Pseudocode Package Does
Common Pseudocode Commands
Numbered Lines and Procedures
The optional number in\begin{algorithmic}[1] controls the line-number interval. Use [1] for every line, [2] for every second line, or omit the option for no line numbers. Use \Procedure for a named block of steps and \Function when the block conceptually returns a value.
algorithm vs algorithmicx vs algpseudocode
algorithmis only the outer float; it does not define\Stateor\If.algorithmicxis the framework on which several pseudocode styles are built.algpseudocodeis the commonly usedalgorithmicxstyle that defines the commands shown on this page.- For most papers and assignments, load
algorithmandalgpseudocodetogether.
Captions, Labels, and Float Placement
Place\caption inside algorithm and add \label immediately after it when you need a cross-reference. Because algorithm is a float, LaTeX may move it to a suitable position. A placement option such as [htbp] expresses a preference; it does not guarantee an exact location.
If the publisher controls algorithm styling, start with its class or template before selecting packages. Some journal classes define their own algorithm environment or disallow conflicting packages.
Alternatives
Common Errors
Pseudocode Writing Practices
- Describe the algorithm rather than the syntax of one programming language.
- Use consistent capitalization for commands, variables, and procedure names.
- Keep each
\Statefocused on one logical action. - Add line numbers when the surrounding text refers to specific steps.
- Use a caption that explains the algorithm’s purpose, not merely “Pseudocode.”
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.Try Pseudocode in LaTeX Cloud Studio
Compile the pseudocode example
Paste the complete example into the browser editor and adapt the procedure, caption, and line numbering.
Compare real code listings
Use listings or minted when the document should show source code instead of language-neutral pseudocode.
