subalgoritmos usando algoritmos, parte a parte b

subalgoritmos usando algoritmos, parte a parte b

Atualmente estou exibindo algoritmos em uma sequência de 1 a 9. Agora eu tenho, por exemplo, o algoritmo 7 e gostaria de chamá-lo de algoritmo 7a e o próximo será o algoritmo 7b e depois o algoritmo 8 novamente. A seguir está o código que eu uso:

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

Também tenho o seguinte:

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

Estou usando-o sem \begin{algorithm}porque dessa forma meus algoritmos seguem bem para a próxima página e não flutuam.

Alguém pode sugerir uma solução para ter os algoritmos 7a e 7b na sequência?

Responder1

O exemplo mínimo a seguir define um subalgorithmsambiente com código obtido literalmente deamsmath.dtx(para o subequationsmeio ambiente). Ele permite ao usuário cercar aqueles algorithms que deveriam ser subenumerados, misturados com algorithms enumerados regularmente:

insira a descrição da imagem aqui

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

Observe que colocar \captionoffora de uma contenção (grupo ou caixa) pode causar problemas de referência.

informação relacionada