3

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.

Paulo Cereda
  • 44,220
Hiren
  • 33
  • 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

2 Answers2

1

You could use something like this:

\newcounter{hdps}
\newenvironment{asm}
  {\refstepcounter{hdps}%
    \renewcommand*{\algorithmcfname}{ASM Spec.}%
    \begin{algorithm}\renewcommand\thealgocf{\arabic{hdps}}}
  {\end{algorithm}\addtocounter{algocf}{-1}}

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

Gonzalo Medina
  • 505,128
  • To get the float placement to work properly, I'd make a minor change:
    \newcounter{hdps}
    \newenvironment{asm}
      {\refstepcounter{hdps}%
        \renewcommand*{\algorithmcfname}{ASM Spec.}%
        \renewcommand\thealgocf{\arabic{hdps}}\begin{algorithm}}
      {\end{algorithm}\addtocounter{algocf}{-1}}
    
    – Hiren Aug 22 '11 at 08:16
1

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
\countstyle{<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.).

Werner
  • 603,163