# Changing caption numbering for algorithm in algorithm2e

I am trying to define a new environment that uses the algorithm environment from algorithm2e. I want to use a separate caption counter for this new environment. As of now, this is what I have, but I don't know how to \def or \renewcommand the caption variable with the hdps counter?

\newcounter{hdps}
\newenvironment{asm}{%
\stepcounter{hdps}%
\renewcommand*{\algorithmcfname}{ASM Spec.}%
\begin{algorithm}}
{%
\def\thealgocf{}%
\end{algorithm}}


Help is much appreciated.

-
Do you want your algorithms to be sequentially numbered with a chapter, or section, or just have a number on their own? –  Werner Aug 21 '11 at 22:06
Sequentially numbered. Thanks. –  Hiren Aug 21 '11 at 22:15

You could use something like this:

\newcounter{hdps}
\newenvironment{asm}
{\refstepcounter{hdps}%
\renewcommand*{\algorithmcfname}{ASM Spec.}%
\begin{algorithm}\renewcommand\thealgocf{\arabic{hdps}}}


EDIT: of course, this approach will only work as expected if \caption is used consistently in the algorithm and asm environments.

-
Great. This works. Thanks =) –  Hiren Aug 21 '11 at 22:19

Using an environment within an environment is sometimes problematic. See the related post Defining environments based on other ones: What's the right way?. In that sense, rather than defining a new environment and using a separate counter, I suggest using the counter provided by the algorithm environment: algocf, and modify both the algorithm name (from Algorithm to ASM Spec.) and the counter style (if needed). For example,

\renewcommand*{\algorithmcfname}{ASM Spec.}% Algorithm name
\usepackage{dcounter}% http://ctan.org/pkg/dcounter
\counterstyle{<section>}%
\DeclareDynamicCounter{algocf}% <- algocf will be 'dynamic' within <section>


will reset the algocf counter within <section> (which could be any sectional unit that you specify, for example chapter, or section, or subsection, or subsubsection). This also redefines the presentation of the counter to be \the<section>.\arabic{algocf}.

Of course, this will not work if you want two different style algorithms (say named Algorithm and ASM Spec.).

-