算法子编号但不基于使用 algorithmicx 的部分

算法子编号但不基于使用 algorithmicx 的部分

我正在写一篇论文,试图更新算法以提高其性能。我希望原始算法的编号为“算法 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}

和输出

在此处输入图片描述

相关内容