
我目前按順序顯示演算法 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,與定期枚舉的algorithm
s 混合:
\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
在容器(組或框)之外可能會導致參考問題。