使用算法的子算法,a 部分 b 部分

使用算法的子算法,a 部分 b 部分

我目前正在按 1-9 的顺序显示算法。现在我有例如算法 7,我想将其命名为算法 7a,下一个将是算法 7b,然后再次是算法 8。以下是我使用的代码:

\captionof{algorithm}{Consistency checker}
\begin{algorithmic}[1]
\Procedure{$\mathbf{Consistent}$}{H}
\ForEach {$n \in H$}    \Comment \emph{go through all the nodes in the graph H}
    \If {there exists a formula $\psi \in H(n)$ AND $\neg\psi \in H(n)$}
        \State \textbf{return} No;
    \EndIf  
\EndFor
\State \textbf{return} Yes;
\EndProcedure
\end{algorithmic}

我还有以下内容:

\DeclareCaptionFormat{algor}{%
\hrulefill\par\offinterlineskip\vskip1pt%
\textbf{#1#2}#3\offinterlineskip\hrulefill}
\DeclareCaptionStyle{algori}{singlelinecheck=off,format=algor,labelsep=space}
\captionsetup[algorithm]{style=algori}

我不用它是\begin{algorithm}因为这样我的算法就可以很好地跟踪到下一页并且不会到处浮动。

有人可以建议一个解决方案来将算法 7a 和 7b 包含在序列中吗?

答案1

以下最小示例定义了一个subalgorithms环境,其代码逐字取自amsmath.dtx(针对subequations环境)。它允许用户将那些algorithm应该被子枚举的 s 包围起来,并与常规枚举的algorithms 混合在一起:

在此处输入图片描述

\documentclass{article}

\usepackage{algpseudocode,algorithm,caption}

\DeclareCaptionFormat{algor}{%
  \hrulefill\par\offinterlineskip\vskip1pt%
  \textbf{#1#2}#3\offinterlineskip\hrulefill}
\DeclareCaptionStyle{algori}{singlelinecheck=off,format=algor,labelsep=space}
\captionsetup[algorithm]{style=algori}

\newcounter{parentalgorithm}

\makeatletter
% Code taken from amsmath (http://mirrors.ctan.org/macros/latex/required/amsmath/amsmath.dtx)
% ===========================================================================================
%    \begin{environment}{subalgorithms}
%    \begin{macrocode}
\newenvironment{subalgorithms}{%
%    \end{macrocode}
%    Before sending down the `algorithm' counter to the subordinate
%    level, add 1 using standard \cn{refstepcounter}.
%    \begin{macrocode}
  \refstepcounter{algorithm}%
%    \end{macrocode}
%    Define \cn{theparentalgorithm} equivalent to current
%    \cn{thealgorithm}. \cn{edef} is necessary to expand the current
%    value of the algorithm counter. This might in rare cases cause
%    something to blow up, in which case the user needs to add
%    \cn{protect}.
%    \begin{macrocode}
  \protected@edef\theparentalgorithm{\thealgorithm}%
  \setcounter{parentalgorithm}{\value{algorithm}}%
%    \end{macrocode}
%    And set the algorithm counter to 0, so that the normal incrementing
%    processes in the various algorithm environments will produce the
%    desired results.
%    \begin{macrocode}
  \setcounter{algorithm}{0}%
  \def\thealgorithm{\theparentalgorithm\alph{algorithm}}%
  \ignorespaces
}{%
  \setcounter{algorithm}{\value{parentalgorithm}}%
  \ignorespacesafterend
}
\makeatother

\begin{document}

\captionof{algorithm}{Consistency checker}
\begin{algorithmic}[1]
  \Procedure{$\mathbf{Consistent}$}{H}
    \For {$n \in H$}    \Comment \emph{go through all the nodes in the graph~$H$}
      \If {there exists a formula $\psi \in H(n)$ AND $\neg\psi \in H(n)$}
        \State \textbf{return} No;
      \EndIf
    \EndFor
    \State \textbf{return} Yes;
  \EndProcedure
\end{algorithmic}

\begin{subalgorithms}
\captionof{algorithm}{Consistency checker}
\begin{algorithmic}[1]
  \Procedure{$\mathbf{Consistent}$}{H}
    \For {$n \in H$}    \Comment \emph{go through all the nodes in the graph~$H$}
      \If {there exists a formula $\psi \in H(n)$ AND $\neg\psi \in H(n)$}
        \State \textbf{return} No;
      \EndIf
    \EndFor
    \State \textbf{return} Yes;
  \EndProcedure
\end{algorithmic}

\captionof{algorithm}{Consistency checker}
\begin{algorithmic}[1]
  \Procedure{$\mathbf{Consistent}$}{H}
    \For {$n \in H$}    \Comment \emph{go through all the nodes in the graph~$H$}
      \If {there exists a formula $\psi \in H(n)$ AND $\neg\psi \in H(n)$}
        \State \textbf{return} No;
      \EndIf
    \EndFor
    \State \textbf{return} Yes;
  \EndProcedure
\end{algorithmic}
\end{subalgorithms}

\captionof{algorithm}{Consistency checker}
\begin{algorithmic}[1]
  \Procedure{$\mathbf{Consistent}$}{H}
    \For {$n \in H$}    \Comment \emph{go through all the nodes in the graph~$H$}
      \If {there exists a formula $\psi \in H(n)$ AND $\neg\psi \in H(n)$}
        \State \textbf{return} No;
      \EndIf
    \EndFor
    \State \textbf{return} Yes;
  \EndProcedure
\end{algorithmic}

\end{document}

请注意,放置\captionof在容器(组或盒子)之外可能会导致引用问题。

相关内容