漂亮地打印溢出到下一页的长算法

漂亮地打印溢出到下一页的长算法

我一直在尝试漂亮地打印一个相当长并与下一页重叠的算法。

algpsudocodex我可以这样做可破坏算法,但我不喜欢它的外观。

tcolorbox可以创建一个看起来很漂亮的算法环境。但即使有这个选项,它也不会溢出到下一页breakable

我的最小示例有三页。算法 1 是breakablealgorithm并且 正确重叠到第 2 页。第 3 页有我自己的tcbalgorithm(结合breakablealgorithmtcolorbox),但如果我在源代码中增加 到\vspace{16cm}\vspace{17cm}整个算法将跳转到第 4 页。

有没有办法将两者结合起来,并使其tcbalgorithm表现得像breakablealgorithm

\documentclass[12pt,a4paper]{article}

\usepackage{xcolor}
\usepackage{algorithm}
\usepackage{algpseudocodex}
\usepackage[breakable]{tcolorbox}

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

\newif\iftkcaption
\newenvironment{tcbalgorithm}[1][]
  {% \begin{breakablealgorithm}
  \tkcaptiontrue
   \begin{center}
     \refstepcounter{algorithm}% New algorithm
     \ifx\relax#1\relax
         \begin{tcolorbox}
     \else
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}#1}%
         \begin{tcolorbox}[title={\textbf{\fname@algorithm~\thealgorithm} #1}]
     \fi
  }{% \end{breakablealgorithm}
     \end{tcolorbox}
   \end{center}
  }
\makeatother

\begin{document}

Top of page
\vspace*{18.5cm}
\begin{breakablealgorithm}
  \caption{This algorithm crosses page boundaries}
  \label{alg:bk}
  \begin{algorithmic}[1]
    \State This algorithm has four lines and overflows to the next page
    \State This is line two
    \State This is line three
    \State This is line four
  \end{algorithmic}
\end{breakablealgorithm}  

Referencing Algorithm~\ref{alg:bk}.

\newpage
Top of page

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{16cm} % replace with 17cm and the colored box will move to the next page
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tcbalgorithm}[This algorithm does not cross page boundaries]
  \label{alg:tk}
  \begin{algorithmic}[1]
    \State Replace the \verb!\vspace{16cm}! above with \verb!\vspace{17cm}! 
    \State and this box will move to the next page
    \State This is line three
    \State This is line four
  \end{algorithmic}
\end{tcbalgorithm}  

Referencing Algorithm~\ref{alg:tk}.

\end{document}

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

编辑: 上面的代码被很好地打包到了一个名为有色定理,有GitHub加拿大运输安全局

答案1

正如我在评论中所说,您没有在定义breakable中添加选项tcolorbox

以下是一个例子:

\documentclass[12pt,a4paper]{article}
\usepackage{xcolor}
\usepackage{algorithm}
\usepackage{algpseudocodex}
\usepackage[breakable]{tcolorbox}
\makeatletter
\newenvironment{tcbalgorithm}[1][]
  {%
  \refstepcounter{algorithm}% New algorithm
     \ifx\relax#1\relax
         \begin{tcolorbox}[breakable]
     \else
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}#1}%
         \begin{tcolorbox}[breakable,title={\textbf{\fname@algorithm~\thealgorithm} #1}]
     \fi
  }{%
  \end{tcolorbox}
  }
\makeatother

\begin{document}
\listofalgorithms\clearpage
Top of page
\vspace{17.5cm}

\begin{tcbalgorithm}[This algorithm does not cross page boundaries]
  \label{alg:tk}
  \begin{algorithmic}[1]
    \State Replace the \verb!\vspace{16cm}! above with \verb!\vspace{17cm}! 
    \State and this box will move to the next page
    \State This is line three
    \State This is line four
  \end{algorithmic}
\end{tcbalgorithm}  

Referencing Algorithm~\ref{alg:tk}.
\vspace{16.5cm}

\begin{tcbalgorithm}
  \label{alg:tk2}
  \begin{algorithmic}[1]
    \State Replace the \verb!\vspace{16cm}! above with \verb!\vspace{17cm}! 
    \State and this box will move to the next page
    \State This is line three
    \State This is line four
  \end{algorithmic}
\end{tcbalgorithm}  

\end{document}

在此处输入图片描述

相关内容