数字算法浮点数与依赖性(子算法环境)

数字算法浮点数与依赖性(子算法环境)

我有 3 种算法,如下所示:

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{algorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

我想要的是:我希望算法 1 的编号方式与文档中其他算法的编号方式相同,例如算法 1然后我希望算法 2 和算法 3 被编号算法1a(或 1.1、1-a 或其他)以及算法 1b

然后我希望编号继续像以前一样,以便下一个算法是算法2。这能做到吗?如果能,怎么做?

答案1

subequations你可以借用amsmath

\documentclass{article}
\usepackage{algorithm}

\makeatletter
\newcounter{parentalgorithm}
\newenvironment{subalgorithms}{%
  \refstepcounter{algorithm}%
  \protected@edef\theparentalgorithm{\thealgorithm}%
  \setcounter{parentalgorithm}{\value{algorithm}}%
  \setcounter{algorithm}{0}%
  \def\thealgorithm{\theparentalgorithm\alph{algorithm}}%
  \ignorespaces
}{%
  \setcounter{algorithm}{\value{parentalgorithm}}%
  \ignorespacesafterend
}
\makeatother

\begin{document}

\begin{subalgorithms}

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{algorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

\end{subalgorithms}

\end{document}

答案2

以下是“简单”的实现方法:

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{algorithm}
\renewcommand{\thealgorithm}{1a}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\renewcommand{\thealgorithm}{1b}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

\begin{algorithm}
\setcounter{algorithm}{1}
\caption{Caption Four}
...
\label{alg:four}
\end{algorithm}

正确的做法可能是创建打开和关闭子编号的宏。

\makeatletter
\newcounter{subalgorithm}
\newenvironment{subalgorithms}{
  \setcounter{subalgorithm}{0}
  % redefine the macro that sets the algorithm number to include the subalgorithm number
  \renewcommand{\thealgorithm}{\arabic{algorithm}\alph{subalgorithm}}%
  % patch the macro that sets the caption head to unincrement to
  % - unincrement the algorithm counter
  % - increment the subalgorithm counter
  % - then do whatever it was supposed to do before
  \let\old@fnum@algorithm=\fnum@algorithm%
  \def\fnum@algorithm{%
    \addtocounter{algorithm}{-1}%
    \stepcounter{subalgorithm}%
    \old@fnum@algorithm}%
}{}
\makeatother

\begin{document}

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm} 

\begin{subalgorithms}

\begin{algorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

\end{subalgorithms}

\begin{algorithm}
\caption{Caption Four}
...
\label{alg:four}
\end{algorithm}

第三种方法是创建一个subalgorithm带有计数器的新浮点数,该计数器用 重置algorithm

\makeatletter
\floatstyle{\ALG@floatstyle}
\newfloat{subalgorithm}{htbp}{loa}[algorithm]
\floatname{subalgorithm}{Subalgorithm}
\renewcommand{\thesubalgorithm}{\arabic{algorithm}\alph{subalgorithm}}
\@namedef{l@subalgorithm}{\@dottedtocline{1}{1.5em}{2.3em}}%
\makeatother

\begin{document}
\listofalgorithms

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{subalgorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{subalgorithm}

\lipsum[2-4]

\begin{subalgorithm}
\caption{Caption Three}
...
\label{subalg:three}
\end{subalgorithm}

\begin{algorithm}
\caption{Caption Four}
...
\label{alg:four}
\end{algorithm}

参考:

相关内容