![算法子编号但不基于使用 algorithmicx 的部分](https://linux22.com/image/418776/%E7%AE%97%E6%B3%95%E5%AD%90%E7%BC%96%E5%8F%B7%E4%BD%86%E4%B8%8D%E5%9F%BA%E4%BA%8E%E4%BD%BF%E7%94%A8%20algorithmicx%20%E7%9A%84%E9%83%A8%E5%88%86.png)
我正在写一篇论文,试图更新算法以提高其性能。我希望原始算法的编号为“算法 2”,改进后的算法应为“算法 2.1”、“算法 2.2”等。使用 algorithmicx 可以实现这一点吗?这些算法并不都位于同一节中。
\documentclass[12pt]{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\section{Tail move reversal algorithm}
\begin{algorithm}[H]
\label{alg:reversal}
\caption{Tail move reversal}
\begin{algorithmic}[1]
\State $x = 0$
\end{algorithmic}
\end{algorithm}
\section{Tail move rearrangement algorithm} %this is the original algorithm
\begin{algorithm}[H]
\label{alg:rearrangement}
\caption{Tail move rearrangement}
\begin{algorithmic}[1]
\State $y = 1$
\State $x = 1$
\State apply Algorithm \ref{alg:reversal} to $x$
\end{algorithmic}
\end{algorithm}
\section{First improvement} %I'd like this one to be numbered 2.1
\begin{algorithm}[H]
\caption{First improvement Algorithm \ref{alg:rearrangement}}
\begin{algorithmic}[1]
\State $y = 2$
\State $x = 3$
\State apply Algorithm \ref{alg:reversal} to $x$
\end{algorithmic}
\end{algorithm}
\section{Second improvement} %I'd like this one to be numbered 2.2
\begin{algorithm}[H]
\begin{algorithmic}[1]
\caption{Second improvement Algorithm \ref{alg:rearrangement}}
\State $y = 2$
\State $x = 3$
\State apply Algorithm \ref{alg:reversal} to $x$
\end{algorithmic}
\end{algorithm}
\end{document}
答案1
这里提供了一个新命令\setAlgoImprovement{<label of algo being improved>}
。需要再次运行。请参阅使用示例
\documentclass[12pt]{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{refcount} % for \getrefnumber
\makeatletter
% #1 = the label of original algorithm
% must be used before \caption
\newcommand{\setAlgoImprovement}[1]{%
\edef\@tempa{\getrefnumber{#1}}%
\ifnum\@tempa=0\relax
% label #1 is not defined, first run
\else
\ifcsname c@algo\@tempa:imp\endcsname
\else
% define new counter "algo<n>:imp" for improvement of algorithm <n>
\newcounter{algo\@tempa:imp}%
\fi
\stepcounter{algo\@tempa:imp}%
\edef\x{%
\noexpand\renewcommand\noexpand\thealgorithm{\noexpand\getrefnumber{#1}.\noexpand\arabic{algo\@tempa:imp}}%
}\x
\fi
}
\makeatother
\begin{document}
\section{Tail move reversal algorithm}
\begin{algorithm}[H]
\label{alg:reversal}
\caption{Tail move reversal}
\begin{algorithmic}[1]
\State $x = 0$
\end{algorithmic}
\end{algorithm}
\section{Tail move rearrangement algorithm} %this is the original algorithm
\begin{algorithm}[H]
\label{alg:rearrangement}
\caption{Tail move rearrangement}
\begin{algorithmic}[1]
\State $y = 1$
\State $x = 1$
\State apply Algorithm \ref{alg:reversal} to $x$
\end{algorithmic}
\end{algorithm}
\section{First improvement} %I'd like this one to be numbered 2.1
\begin{algorithm}[H]
\setAlgoImprovement{alg:rearrangement}
\caption{First improvement Algorithm \ref{alg:rearrangement}}
\begin{algorithmic}[1]
\State $y = 2$
\State $x = 3$
\State apply Algorithm \ref{alg:reversal} to $x$
\end{algorithmic}
\end{algorithm}
\section{Second improvement} %I'd like this one to be numbered 2.2
\begin{algorithm}[H]
\begin{algorithmic}[1]
\setAlgoImprovement{alg:rearrangement}
\caption{Second improvement Algorithm \ref{alg:rearrangement}}
\State $y = 2$
\State $x = 3$
\State apply Algorithm \ref{alg:reversal} to $x$
\end{algorithmic}
\end{algorithm}
\end{document}
和输出