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 want to change the first number of the line numbering in my algorithm, but how is it done? This is my example:

\documentclass[12pt]{beamer}
\usepackage{amsmath, amsfonts, amssymb, amsthm}
\usepackage{algorithmic, algorithm}
\usepackage{float}
\renewcommand\thealgorithm{}

    \begin{document}
    \begin{frame}[t, fragile]
    \frametitle{...}
       \begin{algorithm}[H]
       \caption{...}
       \begin{algorithmic}[1]
          \STATE ...
       \end{algorithmic}
       \end{algorithm}
    \end{frame}
    \end{document}

The first state should be numbered with 2 for example.

share|improve this question
    
Welcome to TeX.sx! –  percusse Nov 2 '12 at 16:49

1 Answer 1

up vote 6 down vote accepted

The line number in algorithmic (from the algorithms bundle) is governed by the counter ALC@line. The following MWE defines \setalglineno{<num>} which allows you to modify the number of any line in your algorithmic:

enter image description here

\documentclass[12pt]{beamer}
\usepackage{algorithmic, algorithm}% http://ctan.org/pkg/algorithms
\usepackage{float}% http://ctan.org/pkg/float
\renewcommand\thealgorithm{}
\makeatletter
\newcommand{\setalglineno}[1]{%
  \setcounter{ALC@line}{\numexpr#1-1}}
\makeatother
\begin{document}
\begin{frame}[t, fragile]
  \frametitle{...}
  \begin{algorithm}[H]
    \caption{...}
    \begin{algorithmic}[1]
      \setalglineno{2}% Set following line number to 2
      \STATE ...
    \end{algorithmic}
  \end{algorithm}
\end{frame}
\end{document}

In fact, \setalglineno{<num>} sets ALC@line to <num>-1, since the algorithm is set in a list, where each line counter is incremented before it is set. So, technically, to obtain a line numbered 2, you need to set the counter value to 1.

A similar setup would be used within the algpseudocode (or similar) environments offered by algorithmicx.

share|improve this answer
    
Is it ALG@line instead of ALC@line? –  hengxin Oct 31 '13 at 3:39
    
@hengxin: It's ALC@line. Try the other one and you'll see why. –  Werner Oct 31 '13 at 4:46
    
ALG@line (not ALC@line) works for me. Maybe it is because that I use algpseudocode + algorithmicx + algorithm environments. –  hengxin Oct 31 '13 at 5:22
    
@hengxin: Yes, there's a difference. –  Werner Oct 31 '13 at 6:00
    
Hi, thanks for an answer. I have a follow up question. What changes should be done to your code, to enable me to pass \ref{} as a parameter to \setalglineno? I want to make a \label at the last line of the first algorithm and the second one would continue its numbering from there. –  jutky Jul 19 at 20:13

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.