The LaTeX packages for typesetting pseudocode carry confusingly similar names — algorithm, algorithmic, algorithmicx (in practice you use algpseudocode), and algorithm2e — and the first stumbling block is working out which of them fight. The genuinely dangerous combination, though, is not one that raises an error. Trying every pairing on TeX Live 2024 showed that loading algorithm2e together with algpseudocode produces no load-time error at all, and then quietly corrupts the output as soon as you write \For. This page settles the two-layer design (container and contents), gives a clash table with the error messages actually reproduced, covers writing \State and \Procedure, shows how to redeclare the keywords yourself, and ends with a recommendation for anyone starting today.
Two layers: the package that makes the box, and the one that writes the body
Typesetting pseudocode splits into two layers with different jobs. The first is the container: a box that floats on the page just like a figure or a table, carries a numbered “Algorithm 1” caption, can be cross-referenced, and can even be collected into a list. That comes from algorithm, which internally builds a new float by calling \newfloat from the float package (algorithm.sty line 31 has \RequirePackage{float}, and lines 82–94 have \newfloat{algorithm}{htbp}{loa}). Its default look is \floatstyle{ruled} — the familiar rules above and below.
The second layer is the contents — commands such as \State (one line), \While (a loop), and \If (a branch) that set the pseudocode itself, complete with indentation and line numbers. Here the choices fork three ways: the old algorithmic, its more flexible successor algorithmicx (in practice you load the layout built on it, algpseudocode), and the separate, self-contained world of algorithm2e. What confuses is not only the names: loading algpseudocode still gives you an environment called algorithmic. The package name and the environment name do not match.
| Package | Layer | Role |
|---|---|---|
algorithm | Container | A float built with \newfloat on top of float. Handles \caption, \label and \listofalgorithms. Pair it with a body package |
algorithmic | Body (old) | The original pseudocode environment; all-uppercase commands (\STATE); barely customisable. Ships in the same algorithms bundle as algorithm |
algpseudocode | Body (current) | The standard layout built on algorithmicx; title-cased commands (\State); the environment is still called algorithmic. algorithmicx is pulled in automatically |
algorithm2e | Both | A self-contained world with its own container and body; its own syntax of \KwIn, \eIf and a line-ending \\;. Use it on its own |
Which combinations break: every pairing tried on TeX Live 2024
The short answer is that exactly one pairing is safe: algorithm + algpseudocode. The others break in three different ways. Here are the messages exactly as they came out of a real run.
% SAFE — the intended pairing, compiles cleanly
\usepackage{algorithm}\usepackage{algpseudocode}
% two body packages: the environment name collides
\usepackage{algorithmic}\usepackage{algpseudocode}
! LaTeX Error: Command \algorithmic already defined.
% algorithm2e already owns a container — order changes only which name trips first
\usepackage{algorithm2e}\usepackage{algorithm}
! LaTeX Error: Command \listofalgorithms already defined.
\usepackage{algorithm}\usepackage{algorithm2e}
! LaTeX Error: Command \algorithm already defined.
! LaTeX Error: Command \algorithm* already defined.Those are the easy failures. LaTeX stops the moment the names collide, so the first line of the log identifies the cause. The problem is the fourth combination: algorithm2e with algpseudocode. Tried in both orders, the number of load-time errors was zero. Worse, if you simply use each environment on its own, both appear to work. It falls apart the instant you write \For inside an algorithmic environment, because the two packages define the same names, \For and \If, in a way that raises no error.
\usepackage{algorithm2e}
\usepackage{algpseudocode} % loads fine. no error. no warning.
...
\begin{algorithmic}[1]
\State $x \gets 0$
\For{$i=1$ to $n$} \State $x \gets x+i$ \EndFor
\end{algorithmic}
% output is scrambled — lines merge and reorder — and only then:
! Missing number, treated as zero.
<to be read again> \ALG@b@2@EndFor@0
l.9 \EndForThe error surfaces at the \EndFor line, phrased as “missing number” — about as far from the real cause as it could be. Nothing points at the two \usepackage lines in the preamble. That is what makes this the worst of the four combinations. Note also that this is a different failure from the option clash you get when two \usepackage calls disagree about options. An option clash fires on a subset test — the second load requests an option the first did not have — and announces itself with the dedicated message Option clash for package. What happens here is a plain duplicate command name: two packages defined the same \For.
Writing an algpseudocode body: from \State to \Procedure
You write pseudocode inside \begin{algorithmic} … \end{algorithmic}, and its optional argument controls line numbering: [0] for none, [1] to number every line, [n] to number every nth. The workhorse is \State, placed once per statement — an assignment, a procedure call. You do not put \State before a block-opening command such as \While or \If, which start a line of their own. Block contents are indented automatically, and whitespace in your source has no effect on the output. The title-cased commands (\State) are the quickest way to tell this apart from the old all-uppercase algorithmic (\STATE, \WHILE).
\State— the start of one statement (one line); used as\State $x \gets 1$.\For{cond}…\EndFor— a loop; the output opens “for … do” and closes “end for”.\ForAll{cond}exists too.\While{cond}…\EndWhile— “while … do” / “end while”.\Repeat…\Until{cond}is also available.\If{cond}…\ElsIf{cond}…\Else…\EndIf— a branch; “if … then”, “else if … then”, “else”, “end if”.\ElsIfand\Elseare optional.\Procedure{name}{args}…\EndProcedure— a procedure; “procedure name(args)” / “end procedure”. The function form\Function{name}{args}…\EndFunctionhas the same shape.\Return— a return value, set as a bold “return” followed by the value.\Comment{...}— an end-of-line comment, placed after a right-pointing triangle ▷.\Require/\Ensure— pre- and post-conditions, each preceded by a bold “Require:” / “Ensure:”.
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
% declare your own keywords: Require/Ensure become Input/Output
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\begin{document}
\listofalgorithms
\begin{algorithm}
\caption{Power}\label{alg:p}
\begin{algorithmic}[1]
\Require $n \ge 0$
\Ensure $y = x^n$
\Procedure{Power}{$x, n$}
\State $y \gets 1$
\While{$n \neq 0$}
\State $y \gets y \times x$ \Comment{one step}
\State $n \gets n - 1$
\EndWhile
\State \Return $y$
\EndProcedure
\end{algorithmic}
\end{algorithm}
See Algorithm~\ref{alg:p}.
\end{document}Compile that twice and you get a floating box ruled above and below, headed “Algorithm 1 Power”. Line numbers 1 to 8 run down the left edge, and the \Require/\Ensure lines come out as “Input:” and “Output:” exactly as redefined — the keywords are not decoration but declarations you can replace. Rewrite \algorithmicwhile, \algorithmicdo, \algorithmicend and the rest the same way and the words for loops and branches change with them. The \listofalgorithms at the top produces a “List of Algorithms” (algorithm.sty keeps a contents file with the extension loa), and \ref{alg:p} in the text resolves to “See Algorithm 1.” Always run it twice so the numbers and cross-references settle.
One point about the container is widely misunderstood, so let it be corrected here. When you write \begin{algorithm}[H] to pin the box in place, you do not need to load the float package yourself. Line 31 of algorithm.sty is \RequirePackage{float}, so float is already there the moment you load algorithm. On TeX Live 2024, a document with nothing but \usepackage{algorithm} and an [H] was confirmed to keep the box fixed between the surrounding paragraphs. While we are here: algorithm takes the options plain, ruled, and boxed to choose the frame style (the default is ruled).
algorithm2e: blocks passed in braces, and the \\; you must not forget
The other major option is algorithm2e. It is self-contained, supplying both container and contents in one package, and is loaded in the preamble as \usepackage[…]{algorithm2e}. Its algorithm environment is the float, so there is no inner environment to nest. The syntax also differs sharply from algpseudocode in three ways. First, input and output use the dedicated commands \KwIn{…} and \KwOut{…} (or \KwData{…} and \KwResult{…}). Second, branches and loops take their body as a braced argument: \eIf{cond}{then part}{else part} (the e means “with else”), \For{cond}{body}, \While{cond}{body}. Third, every statement must end with \\; — forget it and the next statement runs onto the same line.
\documentclass{article}
\usepackage[ruled, vlined, linesnumbered]{algorithm2e}
\SetKwInOut{Param}{Parameters} % declare a keyword of your own
\DontPrintSemicolon % hide the line-ending \;
\begin{document}
\begin{algorithm}[H]
\caption{Sum of positive entries}\label{alg:s}
\KwIn{an array $a[1..n]$}
\Param{tolerance $\epsilon$}
\KwOut{the sum $s$ of its positive entries}
$s \gets 0$\;
\For{$i \gets 1$ \KwTo $n$}{
\eIf{$a[i] > 0$}{
$s \gets s + a[i]$\tcp{keep it}
}{
\tcc{skip}
}
}
\Return $s$\;
\end{algorithm}
See Algorithm~\ref{alg:s}.
\end{document}ruled draws rules above and below with a caption line on top, vlined adds the vertical lines that mark block structure (the command that does the same from the body is \SetAlgoLined, formerly named \SetLine), and linesnumbered numbers each line. \DontPrintSemicolon hides the line-ending \\; from the output, useful when you do not want the pseudocode to look like a programming language. Comments come in two forms: \tcp{…} (the // style, sitting at the end of a line) and \tcc{…} (the /* … */ style, on a line of its own). And \SetKwInOut{Param}{Parameters} is exactly the theme of this page: \KwIn and \KwOut are nothing special — they are made with the same declaration mechanism, and you can add another yourself. \SetKw, \SetKwFunction, and \SetKwData do the same for other kinds of keyword, and the words behind \KwTo and \Return can be replaced too. [H] is implemented by algorithm2e itself, so again there is no need to load float explicitly.
Which to choose if you are starting today
The recommendation is algorithm + algpseudocode, and not as a matter of taste but for three practical reasons. First, it is the only pairing that compiles without a clash on TeX Live 2024. Second, because the container is an ordinary float built on float, \caption, \label, \ref, and \listofalgorithms behave exactly as they do for figures and tables, so there is nothing new to learn. Third, the one-statement-per-\State style is the one most journal and conference templates already assume.
The reasons to choose algorithm2e are equally clear: you want to declare inputs and outputs explicitly, you like passing blocks in braces and the vertical-line layout, or you intend to invent a lot of your own keywords. If any of those fits, it is the more natural choice. Whichever you pick, one rule holds: settle on a single body package for the whole document, and if you use algorithm2e, do not load algorithm. If you want to try both, compare them in separate files rather than mixing them in one — two lines in the preamble will come back to you as an incomprehensible error on the \EndFor line.