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 would like to start the line numbering of the following code not from 1 but from 12 (as it is cut out of a larger code). So it should be 12,13,14,15 instead of 1,2,3,4.

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}

\begin{algorithm}[h]
  \begin{algorithmic}[1]
    \If{$(w,s')$ not in $Q$}                      \Comment{insert}
      \State $k(w,s')  \gets d_{r}(w,s') + \pi_{w,s'}$
    \Else                                        \Comment{decrease}
      \State $k(w,s') \gets d_{r}(w,s')+ \pi_{w,s'}$
    \EndIf
  \end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

share|improve this question
    
Thanks for providing a complete, minimal document! –  Gonzalo Medina Dec 12 '12 at 19:00

1 Answer 1

up vote 3 down vote accepted

Set the ALG@line counter to the desired value:

\documentclass{book}
\usepackage{algorithm,algpseudocode}

\begin{document}

\begin{algorithm}[h]
  \begin{algorithmic}[1]
\makeatletter
\setcounter{ALG@line}{11}
\makeatother
    \If{$(w,s')$ not in $Q$}                      \Comment{insert}
      \State $k(w,s')  \gets d_{r}(w,s') + \pi_{w,s'}$
    \Else                                        \Comment{decrease}
      \State $k(w,s') \gets d_{r}(w,s')+ \pi_{w,s'}$
    \EndIf
  \end{algorithmic}
\end{algorithm}

\end{document}

A better approach would be to use \algstore and \algrestore since this mechanism automatically saves the line number, indentation, open blocks of the current algorithm and closes all blocks; please refer to Section 2.6 Breaking up long algorithms of the algorithmicx documentation.

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.