5

I am trying to define a new environment called protocol, which is the same as the algorithm environment, except for the title and counter. I used this as my guideline. However, it does not reset the counter. Here is my code

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\makeatletter
\newenvironment{protocol}[1][htb]{%
    \renewcommand{\ALG@name}{Protocol}% Update algorithm name
    \begin{algorithm}[#1]%
    }{\end{algorithm}
}
\makeatother

\begin{document}
    \begin{algorithm}
        \caption{Algo Test}
        \label{algo:test}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{algorithm}

    \begin{protocol}
    \caption{Prot Test}
    \label{prot:test}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{protocol}
\end{document} 

Here is my output: Test Output

Protocol and Algorithm environments are both using the same counter. How can I get a separate counter for each?

2 Answers 2

5

The easiest solution is to pretend that the counter is algorithm inside of the protocol environment but is the protocolin fact.

This can be done without any harm with \let\c@algorithm\c@protocol within the protocol environment, since \c@protocol is the internal name of the protocol count register.

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\newcounter{protocol}
\makeatletter
\newenvironment{protocol}[1][htb]{%
  \let\c@algorithm\c@protocol
  \renewcommand{\ALG@name}{Protocol}% Update algorithm name
  \begin{algorithm}[#1]%
  }{\end{algorithm}
}
\makeatother

\begin{document}
See \ref{prot:yetanother}

    \begin{algorithm}
        \caption{Algo Test}
        \label{algo:test}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{algorithm}

    \begin{protocol}
    \caption{Prot Test}
    \label{prot:test}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{protocol}


    \begin{algorithm}
        \caption{Algo Test}
        \label{algo:testother}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{algorithm}

    \begin{protocol}
    \caption{Prot Test}
    \label{prot:testother}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{protocol}

    \begin{protocol}
    \caption{Prot Test}
    \label{prot:yetanother}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{protocol}


\end{document} 

enter image description here

1
  • I agree. this is a better approach.
    – user30471
    Aug 24, 2017 at 23:01
2

It looks like you want to use independent counters for algorithms and protocols. As far as I can see the algorithm package does not allow this and only works with the algorithm counter. So, what you need to do is "trick" the algorithmic environment to use separate counters. Do do this you should set up two new counters: one to keep track of the number of protocols and one to keep track of the algorithms. In the MWE below I have defined them as:

\newcounter{protocol}%        counter for protocols
\newcounter{algorithm saved}% the real counter for algorithms

The algorithmic environment will happily use the algorithm counter. When you switch to your protocol environment you need to switch to using the protocol counter -- and at the same time save the current value of the algorithm counter. If you "swap" back to using the algorithm counter at the end of the protocol environment then everything will remain in sync.

The following MWE does what you want:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\newcounter{protocol}%        counter for protocols
\newcounter{algorithm saved}% the real counter for algorithms

\makeatletter
\newenvironment{protocol}[1][htb]{%
    \renewcommand{\ALG@name}{Protocol}% Update algorithm name
    \setcounter{algorithm saved}{\value{algorithm}} % switch to using the protocol counter
    \setcounter{algorithm}{\value{protocol}}% save the current number of algorithms
    \begin{algorithm}[#1]%
    }{\end{algorithm}
    \setcounter{protocol}{\value{algorithm}}% save the current number of protocols
    \setcounter{algorithm}{\value{algorithm saved}}% restore the algorithm counter
}
\makeatother

\begin{document}
    \begin{algorithm}
        \caption{Algo Test}
        \label{algo:test}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{algorithm}

    \begin{protocol}
    \caption{Prot Test}
    \label{prot:test}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{protocol}

   \begin{algorithm}
        \caption{Algo Test}
        \label{algo:test}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{algorithm}

    \begin{protocol}
    \caption{Prot Test}
    \label{prot:test}
        \begin{algorithmic}
            \State $x \gets y + 1$
        \end{algorithmic}
    \end{protocol}

\end{document}

Here's the output:

enter image description here

6
  • I wouldn't use a space letter inside a counter name. Try \thealgorithm saved just for fun ;-)
    – user31729
    Aug 22, 2017 at 21:38
  • @ChristianHupfer You're right, of course, but in this instance this was a deliberate feature because you don't want anyone to touch the algorithm saved counter. This said, I also think it's kind of cute that this will be fine for a "normal" LaTeX user so long as they stick to using the "advertised" ways of working with counters like \arabic{...}, \value{...}, \stepcounter{...} etc for which this will work fine. Using the etoolbox, you also have \csuse{thealgorithm saved} -- or, \csname thealgorithm saved\endcsname if you prefer.
    – user30471
    Aug 23, 2017 at 4:59
  • I am pretty aware about \csuse or \csname thealgorithm saved\endcsname`, but I don't recommend it anyway
    – user31729
    Aug 23, 2017 at 19:21
  • @ChristianHupfer Of course you are Christian!
    – user30471
    Aug 24, 2017 at 0:11
  • Well, +1 anyway ...
    – user31729
    Aug 24, 2017 at 6:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .