# New algorithm environment with a separate counter

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:

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

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}


• I agree. this is a better approach. – Andrew Aug 24 '17 at 23:01

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:

• I wouldn't use a space letter inside a counter name. Try \thealgorithm saved just for fun ;-) – user31729 Aug 22 '17 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. – Andrew Aug 23 '17 at 4:59
• I am pretty aware about \csuse or \csname thealgorithm saved\endcsname`, but I don't recommend it anyway – user31729 Aug 23 '17 at 19:21
• @ChristianHupfer Of course you are Christian! – Andrew Aug 24 '17 at 0:11
• Well, +1 anyway ... – user31729 Aug 24 '17 at 6:17