使用 algorithmicx 对算法进行标题编号分解

使用 algorithmicx 对算法进行标题编号分解

根据其文档,algorithmicx该包允许分解文档内的算法,如下所示:

Breaking up an algorithm with algorithmicx

相关tex代码为:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Part 1}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
\ForAll {$v \in V(G)$}
\State $l(v) \leftarrow \infty$
\EndFor
\State $l(u) \leftarrow 0$
\Repeat
\For {$i \leftarrow 1, n$}
\State $min \leftarrow l(v_i)$
\For {$j \leftarrow 1, n$}
\If {$min > e(v_i, v_j) + l(v_j)$}
\State $min \leftarrow e(v_i, v_j) + l(v_j)$
\State \Comment For some reason we need to break here!
\algstore{bkbreak}
\end{algorithmic}
\end{algorithm}
And we need to put some additional text between\dots
\begin{algorithm}[h]
\caption{Part 2}
\begin{algorithmic}[1]
\algrestore{bkbreak}
\State $p(i) \leftarrow v_j$
\EndIf
\EndFor
\State $l’(i) \leftarrow min$
\EndFor
\State $changed \leftarrow l \not= l’$
\State $l \leftarrow l’$
\Until{$\neg changed$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

很容易注意到,两段代码之间的标题编号是递增的(第一段代码为算法 3,第二段代码为算法 4)。此外,是否可以为两段代码使用相同的编号?如果可以,我该怎么做?

答案1

作为埃格尔在评论中提到,一个选择是使用

\addtocounter{algorithm}{-1} 

之前。另一个选项是使用包中的 默认标题\caption{Part 2}格式,它不需要手动更改计数器,而且还可以让您为后续算法使用自定义格式。以下是默认标题格式的示例:\ContinuedFloatcaption\ContinuedFloat

\documentclass{article}
\usepackage{caption}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Part 1}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
\ForAll {$v \in V(G)$}
\State $l(v) \leftarrow \infty$
\EndFor
\State $l(u) \leftarrow 0$
\Repeat
\For {$i \leftarrow 1, n$}
\State $min \leftarrow l(v_i)$
\For {$j \leftarrow 1, n$}
\If {$min > e(v_i, v_j) + l(v_j)$}
\State $min \leftarrow e(v_i, v_j) + l(v_j)$
\State \Comment For some reason we need to break here!
\algstore{bkbreak}
\end{algorithmic}
\end{algorithm}
And we need to put some additional text between\dots
\begin{algorithm}[h]
\ContinuedFloat
\caption{Part 2}
\begin{algorithmic}[1]
\algrestore{bkbreak}
\State $p(i) \leftarrow v_j$
\EndIf
\EndFor
\State $l’(i) \leftarrow min$
\EndFor
\State $changed \leftarrow l \not= l’$
\State $l \leftarrow l’$
\Until{$\neg changed$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

enter image description here

修改连续浮点数标题格式的示例:

\documentclass{article}
\usepackage{caption}
\usepackage{algorithm}
\usepackage{algpseudocode}

\makeatletter
\DeclareCaptionLabelFormat{algcontinued}{\ALG@name~#2 (Continued)}
\makeatother
\captionsetup[ContinuedFloat]{labelformat=algcontinued}

\begin{document}
\begin{algorithm}
\caption{Part 1}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
\ForAll {$v \in V(G)$}
\State $l(v) \leftarrow \infty$
\EndFor
\State $l(u) \leftarrow 0$
\Repeat
\For {$i \leftarrow 1, n$}
\State $min \leftarrow l(v_i)$
\For {$j \leftarrow 1, n$}
\If {$min > e(v_i, v_j) + l(v_j)$}
\State $min \leftarrow e(v_i, v_j) + l(v_j)$
\State \Comment For some reason we need to break here!
\algstore{bkbreak}
\end{algorithmic}
\end{algorithm}
And we need to put some additional text between\dots
\begin{algorithm}[h]
\ContinuedFloat
\caption{Part 2}
\begin{algorithmic}[1]
\algrestore{bkbreak}
\State $p(i) \leftarrow v_j$
\EndIf
\EndFor
\State $l’(i) \leftarrow min$
\EndFor
\State $changed \leftarrow l \not= l’$
\State $l \leftarrow l’$
\Until{$\neg changed$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

enter image description here

答案2

在第二个环境中\addtocounter{algorithm}{-1}发出命令之前添加即可解决问题。\captionalgorithm

然而,如果使用这个包,这(以及这种\ContinuedFloat方法)将会破坏链接机制。hyperref

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

%\usepackage{hyperref}

%%% add the following two lines if hyperref is used
%\newcounter{Halgorithm}
%\floatevery{algorithm}{\stepcounter{Halgorithm}}

\begin{document}
\begin{algorithm}
\caption{Part 1}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
\ForAll {$v \in V(G)$}
\State $l(v) \leftarrow \infty$
\EndFor
\State $l(u) \leftarrow 0$
\Repeat
\For {$i \leftarrow 1, n$}
\State $min \leftarrow l(v_i)$
\For {$j \leftarrow 1, n$}
\If {$min > e(v_i, v_j) + l(v_j)$}
\State $min \leftarrow e(v_i, v_j) + l(v_j)$
\State \Comment For some reason we need to break here!
\algstore{bkbreak}
\end{algorithmic}
\end{algorithm}
And we need to put some additional text between\dots
\begin{algorithm}
\addtocounter{algorithm}{-1}
\caption{Part 2}
\begin{algorithmic}[1]
\algrestore{bkbreak}
\State $p(i) \leftarrow v_j$
\EndIf
\EndFor
\State $l’(i) \leftarrow min$
\EndFor
\State $changed \leftarrow l \not= l’$
\State $l \leftarrow l’$
\Until{$\neg changed$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

相关内容