[Tex/LaTex] Suppress line numbering for specific lines in listings package

line-numberinglistings

I would like to suppress the line numbering for specific lines in listings package by identifying the specific lines using some kind of command.

Best Answer

It isn't such simple as it sounds. You have to hook every line. Here a small solution which needs more testing:

enter image description here

\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}
First line.
Second line.|\Suppressnumber|
Third line.
Next line.|\Reactivatenumber|
Next Line
\end{lstlisting}
\end{document}

Next time please provide an example. I took this one from you previous question.