问题拆分两页或多页的非常长的算法

问题拆分两页或多页的非常长的算法

在下面的例子中,我有一个很长的算法,可能跨越 3 页。我尝试手动拆分代码,但每次都会生成一个新的算法编号。如何通过标题“算法 1 继续”并在三页之间保持相同的算法编号,同时让算法行号从最后一页继续?

另一个问题是这三种算法都出现在目录 (TOC) 中。如何修复代码,以便目录中只出现“算法 1”?

以下是代码:

\documentclass{article}
\usepackage{amsmath, amsfonts, amssymb, amsthm, bm}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{tikzmark,calc,,arrows,shapes,decorations.pathreplacing}
\tikzset{every picture/.style={remember picture}}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\definecolor{mybluei}{RGB}{0,173,239}

\def\justifiedandcolored#1{%
  \setlength\fboxrule{0pt}%
  \setlength\fboxsep{0pt}%
  \tikz[background rectangle/.style={top color=red!20,bottom color=white},
  tight background,
  show background rectangle]
  \node [inner sep=0pt] (0,0) {#1};%
}

\makeatletter
\def\munderbar#1{\underline{\sbox\tw@{$#1$}\dp\tw@\z@\box\tw@}}
\makeatother

\begin{document}
\listofalgorithms
\colorbox{mybluei!05}{\color{black}
\begin{minipage}{\textwidth}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
\begin{algorithm}[H]
\caption{Coolest Algorithm ever}
\lipsum[1-4]
\end{algorithm}
\end{minipage}}

\colorbox{mybluei!05}{\color{black}
\begin{minipage}{\textwidth}
\begin{algorithm}[H]
\caption{Coolest Algorithm ever (continued)}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
Normalization of eigenfaces
\lipsum[1-4]
\end{algorithm}
\end{minipage}}

\colorbox{mybluei!05}{\color{black}
\begin{minipage}{\textwidth}
\begin{algorithm}[H]
\caption{Coolest Algorithm ever (continued)}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
Normalization of eigenfaces
\lipsum[1-4]
\end{algorithm}
\end{minipage}}

\end{document} 

答案1

这是实现您想要的方法...

  1. 用于\addtocounter{algocf}{-1}在设置之前减少算法计数器继续 algorithm

  2. 如果您想避免在页面之间跳过时出现“跳跃”效果,请将其用作\strut标题的一部分,以确保它们呈现在相似的高度。

  3. 用 标记算法的结束,\label你可以使用它来设置以下算法的行号refcount\setcounterref{<cntr>}{<label>}

  4. (可选)删除继续\renewcommand{\addcontentsline}[3]{}通过将算法添加到放置算法的组中,可以防止算法被放置在算法列表中继续 algorithms. 由于您正在编写algorithma 内部colorbox,因此此重新定义的范围是有限的。

在此处输入图片描述

\documentclass{article}

\usepackage{lipsum,refcount}
\usepackage[most]{tcolorbox}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

\usetikzlibrary{backgrounds}
\usetikzlibrary{tikzmark,calc,,arrows,shapes,decorations.pathreplacing}
\tikzset{every picture/.style={remember picture}}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\SetNlSty{textbf}{}{:}%

\definecolor{mybluei}{RGB}{0,173,239}

\begin{document}

\listofalgorithms

\clearpage

\colorbox{mybluei!5}{\color{black}%
  \begin{algorithm}[H]
    \caption{Coolest Algorithm ever\strut}
    \lipsum[1-4]
    \label{alg:last-1}%
  \end{algorithm}%
}

\colorbox{mybluei!5}{\color{black}%
  \addtocounter{algocf}{-1}%
  \renewcommand{\addcontentsline}[3]{}%
  \begin{algorithm}[H]
    \caption{Coolest Algorithm ever (continued)\strut}
    \setcounterref{AlgoLine}{alg:last-1}%
    \lipsum[1-4]
    \label{alg:last-2}%
  \end{algorithm}%
}

\colorbox{mybluei!5}{\color{black}%
  \addtocounter{algocf}{-1}%
  \renewcommand{\addcontentsline}[3]{}%
  \begin{algorithm}[H]
    \caption{Coolest Algorithm ever (continued)\strut}
    \setcounterref{AlgoLine}{alg:last-2}%
    \lipsum[1-4]
  \end{algorithm}%
}

\end{document} 

相关内容