Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
Great. This works. Thanks =) –  Hiren Aug 21 '11 at 22:19
    
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 8:16
add comment

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.).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.