17

Is there an option in algorithm2e to split the algorithm over several pages? Please no answer with "use algorithmic or listings packages". algorithm2e is the best package for algorithms in latex, so I want to get it running with this package.

Martin Scharrer
  • 262,582
Ares
  • 171
  • 5
    “algorithm2e is the best package for algorithms in latex” – actually, no; it’s definitely not. algorithmicx (note the trailing “x”!) is far superior – Konrad Rudolph May 24 '11 at 14:00
  • Also a package which has "2e" in its name to indicate that it is for LaTeX2e and not for LaTeX2.x gives me the impression that it is rather old. (@Konrad) – Martin Scharrer May 24 '11 at 14:28

2 Answers2

15

From the algorithm2e documentation:

Caution: algorithms cannot be cut

You could use instead the algorithmicx package which offers you improved functionality and offers the possibility of breaking long algorithms (see Section 2.6 Breaking up long algorithms of the package documentation).

Gonzalo Medina
  • 505,128
  • Not much better though, it still requires you to manually break the algorithm block. Could mean massive reflow of your document. – Code Different Nov 15 '13 at 02:42
  • 8
    algorithmicx hasn't been maintained since 2005! algorithm2e however is still active (check CTAN), so in my view it is the best package available. – MP0 Aug 11 '16 at 12:44
10

A workaround for the problem is to find good place to break, split the algorithm, and use the next to

\setcounter{AlgoLine}{20}

The problem with embedded blocks could be solved with invisible begins/ends. The Begin keyword can be hidden by:

\SetKwBlock{Begin}{}{end}

Example:

The first part:

\begin{algorithm}
\LinesNumbered
% This is to hide end and get the last vertical line straight
\SetKwBlock{Begin}{Begin}{}
\SetAlgoLined
\Begin{
% This is to restore vline mode
  \SetAlgoVlined
  $\mathcal{E} \leftarrow \emptyset$\;

The second part:

\begin{algorithm}
  \LinesNumbered
\setcounter{AlgoLine}{19}
%This is to hide Begin keyword
\SetKwBlock{Begin}{}{end}
\Begin{

Update the example code:

    \documentclass{report}

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

\begin{document}

\begin{algorithm} \LinesNumbered % This is to hide end and get the last vertical line straight \SetKwBlock{Begin}{Begin}{} \SetAlgoLined \Begin{ $\mathcal{E} \leftarrow \emptyset$; } \end{algorithm}

\SetNlSty{texttt}{(}{)} \begin{algorithm} \LinesNumbered \setcounter{AlgoLine}{12} % This is to restore vline mode if you did not take the package as \usepackage[linesnumbered,ruled,vlined]{algorithm2e} \SetAlgoVlined %This is to hide Begin keyword \SetKwBlock{Begin}{}{end} \Begin{ $\mathcal{E} \leftarrow \emptyset$; $\mathcal{E} \leftarrow \emptyset$; $\mathcal{E} \leftarrow \emptyset$; $\mathcal{E} \leftarrow \emptyset$; $\mathcal{E} \leftarrow \emptyset$;
} \end{algorithm}

\end{document}

enter image description here

outmind
  • 377
  • It sounds easy... how about providing an example that does this? The example should include nested blocks of pseudo-code and the algorithm broken across these nested blocks. – Werner Jan 10 '17 at 03:34
  • Probably, an example will obscure the simplicity of the solution. – outmind Jan 10 '17 at 03:44
  • 1
    How would you manage the case where there is a \caption added to the algorithm under the ruled style? – Werner Jan 10 '17 at 03:56
  • Depends on what I want to achieve. Probably I don't see the problem. I would call parts like "Algorithm (Part 1 of 3)" etc... – outmind Jan 10 '17 at 16:54
  • (+1) this worked best for me. – mhdadk Oct 25 '22 at 16:23