对自定义算法环境使用标准算法填充

对自定义算法环境使用标准算法填充

这是一个后续问题:https://tex.stackexchange.com/a/33869/173478

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来修改环境,使其始终具有与环境相同的填充:algorithmcenter

\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

相关内容