Unteralgorithmen mit algorithmischen, Teil A Teil B

Unteralgorithmen mit algorithmischen, Teil A Teil B

Ich zeige derzeit Algorithmen in der Reihenfolge 1-9 an. Jetzt habe ich beispielsweise Algorithmus 7 und möchte ihn Algorithmus 7a nennen, der nächste soll Algorithmus 7b sein und dann wieder Algorithmus 8. Folgendes ist der Code, den ich verwende:

\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}

Außerdem habe ich Folgendes:

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

Ich verwende es ohne, \begin{algorithm}weil meine Algorithmen auf diese Weise problemlos auf der nächsten Seite weiterlaufen und nicht herumschweben.

Kann jemand eine Lösung vorschlagen, um die Algorithmen 7a und 7b in der Sequenz zu haben?

Antwort1

Das folgende Minimalbeispiel definiert eine subalgorithmsUmgebung mit Code, der wortwörtlich übernommen wurde ausamsmath.dtx(für die subequationsUmgebung). Es ermöglicht dem Benutzer, die algorithms, die weiter aufgezählt werden sollen, mit regulär aufgezählten algorithms zu vermischen:

Bildbeschreibung hier eingeben

\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}

Beachten Sie, dass das Platzieren \captionofaußerhalb einer Eingrenzung (Gruppe oder Box) Referenzprobleme verursachen kann.

verwandte Informationen