3

In the example below, I have a very long algorithm, probably spanning 3 pages. I tried manually splitting the code up, but each time, a new algorithm number is generated. How do you maintain the same algorithm number across the span of the three pages by having the caption "Algorithm 1 continued" and also have the algorithm line numbers continue from the last page?

Another issue is that the three algorithm appear in the Table of Contents (TOC). How to fix the code so that only "Algorithm 1" appears in the TOC?

Here is the code:

\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 Answer 1

3
+50

Here is a way to achieve what you want...

  1. Use \addtocounter{algocf}{-1} to reduce the Algorithm counter before setting a countinued algorithm.

  2. Use \strut as part of the caption to ensure that they present at a similar height if you want to avoid a "jumping" effect when skipping between pages.

  3. Mark the end of an algorithm with a \label that you can use to set the line number of the following algorithm via refcount's \setcounterref{<cntr>}{<label>}.

  4. (Optional) Remove continued algorithms from being placed in the List of Algorithms by adding \renewcommand{\addcontentsline}[3]{} within a group where you place the continued algorithms. Since you're writing the algorithm inside a colorbox, this redefinition is limited in scope.

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .