[Tex/LaTex] Pseudo code to be spread over multiple pages

floatspage-breakingpseudocode

I am writing pseudo code in the algorithmic environment. It is quite long and doesn't fit on one single page. Instead of it continuing on the next page, it is cut off. I tried inserting it into a figure environment, but the problem persists. Is there a solution to have the code continue on the next page?

Best Answer

If you do not want the algorithm to float, just use the algorithmic environment without the algorithm environment.

Otherwise, you can use the algorithmicx package. It allows breaking long algorithms in parts, preserving line numbers and indentation. You can also combine this with caption's \ContinuedFloat to get proper algorithm numbering:

\documentclass{article}

\usepackage{algorithm,algpseudocode}
\usepackage{caption}

\begin{document}

\begin{algorithm}
  \caption{My algorithm}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$

        \algstore{myalg}
  \end{algorithmic}
\end{algorithm}

\clearpage

\begin{algorithm}
  \ContinuedFloat
  \caption{My algorithm (continued)}
  \begin{algorithmic}
      \algrestore{myalg}

        \State $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}
      \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\end{document}

(The algorithm example is taken from algorithmicx's documentation.)