Split algorithm2e over two pages within for-block and if-else-block

algorithm2ealgorithmspage-breaking

I do know that this questions have been asked before in

How to split algorithm2e over two pages

Unfortunately the answers given there did not work out for me.

I am struggling to realize a page break at the indicated spot.

Help is much appreciated.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[ruled,vlined,linesnumbered,onelanguage]{algorithm2e}

\begin{document}

\begin{algorithm}[H]        
\SetAlgoLined
\caption{Dummy Algorithm.}
%
Do something. \\
\For{$k=0,1,2,3, ...$}{
    Do something. \\
    \eIf{$x \geq y$}{
         Do something. \\
    }{
         Do something. \\
         \For{$j = 1,...,10$}{
              Do something. \\
         }
         THIS IS THE SPOT WHERE I NEED THE PAGE BREAK.\\
         Do something. \\
    }
}
\end{algorithm}

\end{document}

Best Answer

The linked answer's method will only work if you can break the algorithm into two or more parts that can be compiled separately. This is not the case in your example.

This approach is an alternative to solve the problem.

(1) Generate the algorithm as a standalone pdf file (algo.pdf in this example) using algo.tex.

(2) Add the top part of the algorithm using the trim and clip options of the \includegraphics command to crop the upper part of image.

\includegraphics[trim=left bottom right top, clip] (In bp units. A big point is 1/72 inch)

Add \clearpage to typeset all remaining floats and then include the lower part of the figure, this time choosing the trim values to crop the bottom part of the image.

Finally increase the counter of algorithm by 1.

x

This is the file algo.tex

%% File algo.tex

\documentclass[preview]{standalone}

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

\begin{document}
    
    \begin{algorithm}[H]   
        \SetAlgoLined
        \caption{Dummy Algorithm.}
        %
        Do something. \\
        \For{$k=0,1,2,3, ...$}{
            Do something. \\
            \eIf{$x \geq y$}{
                Do something. \\
            }{
                Do something. \\
                \For{$j = 1,...,10$}{
                    Do something. \\
                }
                THIS IS THE SPOT WHERE I NEED THE PAGE BREAK.\\
                Do something. \\
            }
        }
    \end{algorithm}
    
\end{document}

This is the complete code.

\documentclass[a4paper]{article}
\usepackage[ruled,vlined,linesnumbered,onelanguage]{algorithm2e}% used for the second algorithm

\usepackage{graphicx}
\usepackage[labelsep=none,labelformat=empty]{caption}% empty captions
\usepackage{kantlipsum} % only for dummy text

\begin{document}

1.  \kant[1-2]\kant[9]

\noindent
\begin{figure}[bh!]
    \centering
    \includegraphics[trim=0 43 10 0,clip]{algo.pdf}
    \caption{}\label{alg:one}
\end{figure}
\clearpage % typeset all floats
\noindent
\begin{figure}[th!]
    \centering
    \includegraphics[trim=0 0 10 150,clip]{algo.pdf}
\end{figure}
\stepcounter{algocf}% correct the algorithm number
    
See listing of Algorithm~\ref{alg:one}.

\bigskip    
4.  \kant[11]

\begin{algorithm}   
    \SetAlgoLined
    \caption{Another Dummy Algorithm.}
    %
    Do something. \\
    \For{$k=0,1,2,3, ...$}{
        Do something. \\
        \eIf{$x \geq y$}{
            Do something. \\
        }{
            Do something. \\
            \For{$j = 1,...,10$}{
                Do something. \\
            }
            ANOTHER Algorithm.\\
            Do something. \\
        }
    }
\end{algorithm}
\end{document}