我一直在尝试漂亮地打印一个相当长并与下一页重叠的算法。
algpsudocodex
我可以这样做可破坏算法,但我不喜欢它的外观。
我tcolorbox
可以创建一个看起来很漂亮的算法环境。但即使有这个选项,它也不会溢出到下一页breakable
。
我的最小示例有三页。算法 1 是breakablealgorithm
并且 正确重叠到第 2 页。第 3 页有我自己的tcbalgorithm
(结合breakablealgorithm
和tcolorbox
),但如果我在源代码中增加 到\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}
答案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}