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 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?

share|improve this question
1  
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
2  
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

1 Answer 1

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}

enter image description here

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>]
  \IfValueTF{#1}{\makeatletter\setcounter{ALG@line}{#1}\addtocounter{ALG@line}{-1}\makeatother}{}%
  \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}

enter image description here

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}

enter image description here

share|improve this answer

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.