我正在尝试定义一个名为 protocol 的新环境,它与 algorithm 环境相同,只是 title 和 counter 不同。我使用了这作为我的指导方针。但是,它不会重置计数器。这是我的代码
\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}
协议和算法环境都使用相同的计数器。如何为每个环境获取单独的计数器?
答案1
最简单的解决方案是假装计数器在环境algorithm
内部protocol
,但protocol
事实上并非如此。
由于是计数寄存器的内部名称,因此这可以在不损害\let\c@algorithm\c@protocol
环境的情况下完成。protocol
\c@protocol
protocol
\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}
答案2
看起来你想为algorithms
和分别使用独立的计数器protocols
。据我所知算法包不允许这样做,并且只适用于计数器algorithm
。因此,您需要做的是“欺骗”环境algorithmic
以使用单独的计数器。要做到这一点,您应该设置两个新的计数器:一个用于跟踪协议的数量,另一个用于跟踪算法。在下面的 MWE 中,我将它们定义为:
\newcounter{protocol}% counter for protocols
\newcounter{algorithm saved}% the real counter for algorithms
环境algorithmic
会很乐意使用algorithm
计数器。当您切换到protocol
环境时,您需要切换到使用计数器protocol
——同时保存计数器的当前值。如果您在环境结束时algorithm
“切换”回使用计数器,那么一切都将保持同步。algorithm
protocol
以下 MWE 可以满足您的要求:
\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}
输出如下: