# Algorithm spanning more than one page

I have an algorithm which is too long to fit on one page. Someone suggested splitting the algortithm manually and then altering the second algorithm's line numbers to follow on the first. I know \begin{algorithmic}[1] numbers every line, whereas \begin{algorithmic}[5] numbers every fifth. How to I change the starting number from "1" to something else?

-
Welcome to TeX.sx! A tip: You can use backticks  to mark your inline code as I did in my edit. :) –  Paulo Cereda Oct 15 '11 at 16:08
Without a minimal working example (MWE) or any information what packages you use to create your algorithms it's hard to say. –  lockstep Oct 15 '11 at 16:08

The following minimal example shows how to modify the starting line number for an algorithm. Within the algorithmic environment, add the code

\makeatletter\setcounter{ALG@line}{<n>}\makeatother


where <n> is one smaller than the first line number. The reason for this is because the \State command increments the counter first, before typesetting it.

\documentclass{article}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithmic}[1]
\makeatletter\setcounter{ALG@line}{5}\makeatother
\State Here is some code
\State Here is another line of code
\State And then some more
\end{algorithmic}
\end{document}


This, of course, assumes that you're using the (more advanced) algorithmicx package to typeset your algorithmic environments.

If you would like to make this procedure a little more convenient, it is possible. For example, you can redefine \State to do this for you by providing an optional command:

\documentclass{article}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{xparse}% http://ctan.org/pkg/xparse
\let\oldState\State% Store \State in \oldState
\RenewDocumentCommand{\State}{o}{% \State[<num>]
\oldState\ignorespaces%
}%
\begin{document}
\begin{algorithmic}[1]
\State[8] Here is some code
\State Here is another line of code
\State And then some more
\State[5] and a last line of code
\end{algorithmic}
\end{document}


The interface for redefining \State to \State[<num>] is provided by xparse.

Note that if you plan on breaking up a single algorithmic environment across more than one page, algorithmicx also offers \algstore{<name>} and \algrestore{<name>} options. Here's a short example:

\documentclass{article}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithmic}[1]
\State Here is some code
\State Here is another line of code
\algstore{myalg}% Save algorithm
\end{algorithmic}

\hrule

\begin{algorithmic}[1]
\algrestore{myalg}% Restore algorithm
\State And then some more
\State and a last line of code
\end{algorithmic}
\end{document}
`

-