Werner 提出的环境breakablealgorithm
完美地分割了算法。然而,对我来说,可拆分算法与常规算法具有不同的垂直填充。以下是重现该行为的最小示例:
\documentclass[a4paper]{article}
\usepackage{algorithm,algpseudocode}
\makeatletter
\newenvironment{breakablealgorithm}
{% \begin{breakablealgorithm}
\begin{center}
\refstepcounter{algorithm}% New algorithm
\hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
\renewcommand{\caption}[2][\relax]{% Make a new \caption
{\raggedright\textbf{\fname@algorithm~\thealgorithm} ##2\par}%
\ifx\relax##1\relax % #1 is \relax
\addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
\else % #1 is not \relax
\addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
\fi
\kern2pt\hrule\kern2pt
}
}{% \end{breakablealgorithm}
\kern2pt\hrule\relax% \@fs@post for \@fs@ruled
\end{center}
}
\makeatother
% DOCUMENT
\begin{document}
This is some text.
\begin{algorithm}[H]
\caption{Regular Algorithm}
\begin{algorithmic}
\State Start
\end{algorithmic}
\end{algorithm}
And some more text.
\begin{breakablealgorithm}
\caption{Breakable Algorithm}
\begin{algorithmic}
\State Start
\end{algorithmic}
\end{breakablealgorithm}
And even more text.
\end{document}
另外,将文档类设置为 amsart 或 amsbook 时,根本没有填充。如何修改环境breakablealgorithm
以自然地具有与常规算法环境相同的填充?
答案1
看起来algorithm
环境正在使用\intextsep
数量进行垂直填充。因此,可以通过用适当的垂直间距替换环境breakablealgorithm
来修改环境,使其始终具有与环境相同的填充:algorithm
center
\makeatletter
\newenvironment{breakablealgorithm}
{% \begin{breakablealgorithm}
\par\vspace{\intextsep}
\refstepcounter{algorithm}% New algorithm
\hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
\renewcommand{\caption}[2][\relax]{% Make a new \caption
{\raggedright\textbf{\fname@algorithm~\thealgorithm} ##2\par}%
\ifx\relax##1\relax % #1 is \relax
\addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
\else % #1 is not \relax
\addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
\fi
\kern2pt\hrule\kern2pt
}
}{% \end{breakablealgorithm}
\kern2pt\hrule\relax% \@fs@post for \@fs@ruled
\par\vspace{\intextsep}
}
\makeatother