アルゴリズムを使用したサブアルゴリズム、パート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定期的に列挙される と混在させて、サブ列挙される を囲むことができますalgorithm

ここに画像の説明を入力してください

\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包含範囲 (グループまたはボックス) の外側に配置すると参照の問題が発生する可能性があることに注意してください。

関連情報