Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 10 down vote accepted

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.)

share|improve this answer
    
I am sorry, I cannot get this example to complie exactly as it is or even with modifications. ! LaTeX Error: Something's wrong--perhaps a missing \item. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.12 \State $r\gets a\bmod b$ –  Vass Mar 31 '11 at 17:51
1  
@Vass: Disregard my earlier comment. Which versions of algorithm, algorithmicx and caption do you have (see the log file)? –  Andrey Vihrov Mar 31 '11 at 18:48
    
Here is what is said in the log file for the EXACT code place above for these keywords. Package: algorithmicx 2005/04/27 v1.2 Algorithmicx Document Style algorithmicx 1.2 - a greatly improved algorithmic' style; Document style option algorithm' Release 2.5 -- March 13 1995 --;Package: caption 2007/01/07 v3.0k Customising captions (AR). Thanks –  Vass Apr 3 '11 at 22:12
1  
@Vass: March 13 1995 is obviously very old, and I suspect it is the cause. Update your TeX distribution or just this package to a more reasonable version. –  Andrey Vihrov Apr 3 '11 at 23:32
1  
@AndreyVihrov: is it possible to let TeX do the algorithm breaking automatically? –  Stan Jan 8 '13 at 8:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.