1

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}
4
  • It seems that this answer should be workable for you tex.stackexchange.com/a/371538/2693 so what about it doesn’t work and we can help you with that.
    – Alan Munn
    Commented Mar 25, 2022 at 12:53
  • There \SetKwBlock{Begin}{Begin}{}? and \SetKwProg{Loop}{LOOP}{}{} is used. I need something like this for \For{$k=0,1,2,3, ...$}{...} and \eIf{$x \geq y$}{...Also I already posted this link and. as I said, was not able to make it work. So there is no need to respost it...thanks anyway. Commented Mar 25, 2022 at 15:15
  • What I mean is show how your attempt to use that solution failed.
    – Alan Munn
    Commented Mar 25, 2022 at 16:00
  • Man, what I tried didnt even compile...I am not that good at latex to be honest...How should I use \SetKwBlock{...}{...}{} for the first for-loop in 4? Commented Mar 25, 2022 at 16:16

1 Answer 1

2

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}

You must log in to answer this question.

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