# sub-algorithms using algorithmic, part a part b

I am currently displaying algorithms in a sequence 1-9. Now I have for example algorithm 7 and I would like to name it algorithm 7a and the next one will be algorithm 7b and then algorithm 8 again. The following is the code that I use:

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


Also I have the following:

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


I am using it without \begin{algorithm} because this way my algorithms follow onto next page nicely and they don't float around.

Can someone suggest a solution to have algorithm 7a and 7b in the sequence?

The following minimal example defines a subalgorithms environment with code taken verbatim from amsmath.dtx (for the subequations environment). It allows the user to surround those algorithms that should be sub-enumerated, intermixed with regularly-enumerated 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}


Note that placing \captionof outside of a containment (group or box) may cause reference problems.

• referencing is not an issue, I need to get around what you wrote and test it. From what you presented it looks exactly what I need. Thanks – aki2014 Mar 25 '16 at 2:07
• question is using \begin{subalgorithms}, will it allow to split the algorithm without floating around the page? – aki2014 Mar 25 '16 at 2:10
• @aki2014: Yes. The subalgorithms environment merely sets up the counters and restores it at the end. It doesn't affect the algorithmic` environment at all (which will still break across the page boundary). – Werner Mar 25 '16 at 2:12
• Fantastic, works well. I am accepting your answer. However, I have one last question is it possible to have the algorithms listed for example; 1a,2a,1b,2b ? – aki2014 Mar 25 '16 at 2:21
• @aki2014: Yes you can, but there's not a simple solution (I think). You may be better off asking a follow-up question. – Werner Mar 26 '16 at 4:31