7

Referring to Skip line numbers and resume from specific number I tried to skip several line numbers in my listings and continue with a specific number. But in my case there is a really bad side effect: each line after \Reactivatenumber{...} has the same number. Also new listings have the number defined in the previous \Reactivatenumber{...}

My Example:

\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}
First listing:
\begin{lstlisting}[firstnumber=1]
var myFunc = function() {|\Suppressnumber|
    //my function does many great things
    //and it's only 10 lines long!|\Reactivatenumber{12}|
}

var oneMoreFunc = function() {
    return myFunc;
}
\end{lstlisting}
One more listing:
\begin{lstlisting}[firstnumber=18]
var something = "bla";
console.log(something);
\end{lstlisting}
\end{document}

Result: Result of my example

Can anybody help me please?

6

Change your definition to

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

enter image description here

Full 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]{%
  \setcounter{lstnumber}{\numexpr#1-1\relax}
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
   \refstepcounter{lstnumber}
  }%
}


\makeatother

\begin{document}
First listing:
\begin{lstlisting}[firstnumber=1]
var myFunc = function() {|\Suppressnumber|
    //my function does many great things
    //and it's only 10 lines long!|\Reactivatenumber{12}|
}

var oneMoreFunc = function() {
    return myFunc;
}

}

var oneMoreFunc = function() {
    return myFunc;
}
\end{lstlisting}
One more listing:
\begin{lstlisting}[firstnumber=18]
var something = "bla";
console.log(something);

}

var oneMoreFunc = function() {
    return myFunc;
}
\end{lstlisting}
\end{document}
1

A small addition to to the previous also correct answer. At the end of the line:

\refstepcounter{lstnumber}%

There should be a '%'. Omitting this will result in subsequent inline listings to be improperly formatted.

The reactivate command is then as follows:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.