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.

2
  • 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 6, 2014 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. Dec 6, 2014 at 16:39

1 Answer 1

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}
2
  • Wonderful, exactly what I needed. Cheers. Dec 7, 2014 at 18:51
  • 3
    The line number don't count up after Reactivatenumber (they just continue as 12, 12, 12, 12,...). Solution here: tex.stackexchange.com/a/264373/39635
    – Geier
    Mar 22, 2016 at 8:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .