5

I'm a total latex noob, I would like to accomplish something similar to what has been asked here, but rather than resuming numbering from where it was left off, I'd like to pass the new line number as argument to the \Reactivatenumber command. Sort of like this:

\documentclass{article} 
\usepackage{listings}

\lstset{numbers=left,numberblanklines=false,escapeinside=||}
\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
     \advance\c@lstnumber-\@ne\relax%
    }%
}

\newcommand*\Reactivatenumber{%
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
   \advance\c@lstnumber\@ne\relax}%
}

\makeatother
\begin{document}
\begin{lstlisting}
var myFunc = function() {|\Suppressnumber|
    //my function does many great things
    //and it's only 10 lines long!|\Reactivatenumber{12}|
}
\end{lstlisting}
\end{document}

To produce something like this (pardon formatting):

 1 | var myFunc = function() {
   |    //my function does many great things
   |    //and it's only 10 lines long!
12 | }

What would be the right way to define the \Reactivatenumber command? Cheers.

  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs Dec 06 '14 at 16:08
  • Alright, I apologize for I thought citing a similar question and then just writing the part of code that should differ would suffice; also, please be advised that the complete code, as it looks now, may not compile (though I have not tried), since I wrote |\Reactivatenumber{12}| which is my desiderata, how I'd like to change the command in order to do what I'd like it to. Cheers. – Andrea Aloi Dec 06 '14 at 16:39

1 Answers1

4

You can set the lstnumber counter to one less than the next line number in Reactivatenumber:

enter image description here

Code:

\documentclass{article} 
\usepackage{listings}

\lstset{numbers=left,numberblanklines=false,escapeinside=||} \let\origthelstnumber\thelstnumber \makeatletter \newcommand*\Suppressnumber{% \lst@AddToHook{OnNewLine}{% \let\thelstnumber\relax% \advance\c@lstnumber-@ne\relax% }% }

\newcommand*\Reactivatenumber[1]{% \lst@AddToHook{OnNewLine}{% \let\thelstnumber\origthelstnumber% \setcounter{lstnumber}{\numexpr#1-1\relax}% %\advance\c@lstnumber@ne\relax% }% }

\makeatother

\begin{document} \begin{lstlisting} var myFunc = function() {|\Suppressnumber| //my function does many great things //and it's only 10 lines long!|\Reactivatenumber{12}| } \end{lstlisting} \end{document}

Peter Grill
  • 223,288
  • Wonderful, exactly what I needed. Cheers. – Andrea Aloi Dec 07 '14 at 18:51
  • 3
    The line number don't count up after Reactivatenumber (they just continue as 12, 12, 12, 12,...). Solution here: http://tex.stackexchange.com/a/264373/39635 – Geier Mar 22 '16 at 08:28