# How to customize the number in the caption of an algorithm in algorithm2e

In algorithm2e, if you use the "ruled" option, the caption of algorithms are automatically numbered. E.g. if you write 2 algorithms in one document, the first one will be titled Algorithm 1 and the second one will be titled Algorithm 2. How can I customize this number? Say I only write one algorithm and want it to be titled "Algorithm 3" as shown by the picture below. You could just force the counter associated with that environment.

Note that, in typical LaTeX fashion, counters are incremented just before they're used. So, in order to force the algorithm to start as Algorithm 3 (say), one would set the associated counter to 2. The counter used by algorithm2e for the algorithm environment is algocf (named after the author, Christophe Fiorio, I presume). So, using

\setcounter{algocf}{2}


immediately before using the algorithm environment would set the following algorithm as Algorithm 3.

The following example is taken from the algorithm2e documentation: \documentclass{article}
\usepackage[ruled]{algorithm2e}% http://ctan.org/pkg/algorithm2e
\begin{document}

\setcounter{algocf}{2}% Modify counter of algorithm

\begin{algorithm}
\DontPrintSemicolon
\KwData{$G=(X,U)$ such that $G^{tc}$ is an order.}
\KwResult{$G’=(X,V)$ with $V\subseteq U$ such that $G’^{tc}$ is an
interval order.}
\Begin{
$V \longleftarrow U$\;
$S \longleftarrow \emptyset$\;
\For{$x\in X$}{
$NbSuccInS(x) \longleftarrow 0$\;
$NbPredInMin(x) \longleftarrow 0$\;
$NbPredNotInMin(x) \longleftarrow |ImPred(x)|$\;
}
\For{$x \in X$}{
\If{$NbPredInMin(x) = 0$ {\bf and} $NbPredNotInMin(x) = 0$}{
$AppendToMin(x)$}
}
\nl\While{$S \neq \emptyset$}{\label{InRes1}
\nlset{REM} remove $x$ from the list of $T$ of maximal index\;\label{InResR}
\lnl{InRes2}\While{$|S \cap ImSucc(x)| \neq |S|$}{
\For{$y \in S-ImSucc(x)$}{
\{ remove from $V$ all the arcs $zy$ : \}\;
\For{$z \in ImPred(y) \cap Min$}{
remove the arc $zy$ from $V$\;
$NbSuccInS(z) \longleftarrow NbSuccInS(z) - 1$\;
move $z$ in $T$ to the list preceding its present list\;
\{i.e. If $z \in T[k]$, move $z$ from $T[k]$ to
$T[k-1]$\}\;
}
$NbPredInMin(y) \longleftarrow 0$\;
$NbPredNotInMin(y) \longleftarrow 0$\;
$S \longleftarrow S - \{y\}$\;
$AppendToMin(y)$\;
}
}
$RemoveFromMin(x)$\;
}
}
\caption{IntervalRestriction\label{IR}}
\end{algorithm}

\end{document}

• thanks. it works. Just wonder how do you find out that the counter name is algocf. I couldn't find it in the manual of algorithm2e.
– sma
Jul 5, 2012 at 7:27
• @pegausbupt: The inclusion of this detail depends on the package author. I found this by browsing through the algorithm2e.sty style file (searching for \newcounter).
– Werner
Jul 5, 2012 at 14:07
• Great, thank you. Where did you find out the counter name, I presume you looked at the .sty file? But how exactly do I find out what the counter name is (I'm not a tex guy...) Mar 5, 2020 at 16:59
• @polynomial_donut: Yes, I looked at the source code. The sources are all available on CTAN - the Comprehensive TeX Archive Network. Within algorithm2e.sty, I searched for \newcounter or \newcount (for the counter definition) or \stepcounter or \refstepcounter or \addtocounter (for the counter increment); then, like any debugging approach, you just browse the (hopefully well-documented) code.
– Werner
Mar 5, 2020 at 18:13
• It seems that after setting the counter, the reference number does not change. For example, if the algorithm is labeled as \label{alg}, then \ref{alg} still displays 1 rather than 3. How to solve this problem? Jun 8, 2020 at 4:46

@pegausbupt: How to use the same updated counter number for an Algorithm in citing? For an algorithm with caption Algorithm 3, the new caption is Algorithm 1, but when I cited I still get Algorithm 3. I need suggestions. Tx

\setcounter{algocf}{0} I used this to set counter manually.