I would like to suppress the line numbering for specific lines in listings package by identifying the specific lines using some kind of command.
Asked
Active
Viewed 4,536 times
9
-
6Please accept answers. In your previous question there are 2 answers without any response: http://tex.stackexchange.com/questions/33999/suppress-line-numbering-for-empty-lines-in-listings-package – Marco Daniel Nov 08 '11 at 12:15
1 Answers
17
It isn't such simple as it sounds. You have to hook every line. Here a small solution which needs more testing:

\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.
Werner
- 603,163
Marco Daniel
- 95,681
-
Sorry for not providing an example; I felt my question was simple. Anyhow, thank you very much for the answer. I tried it and got the following result: – user9131 Nov 08 '11 at 17:32
-
I suppose that you do not need the
\globaland why do you have all the\relaxafter theletstatement? – Nov 08 '11 at 17:41 -
@Herbert: The
\relaxis obsolete. I know this. I don't know why I haven't changed it. -- I tested without global and it works. – Marco Daniel Nov 08 '11 at 17:48 -
Everything worked, except the line numbers were not suppressed, but the line number was just repeated so I saw 1 xxx 2 xxx 2 xxx 3 xxx. – user9131 Nov 08 '11 at 17:58
-
1@user9131: Marco's example works fine, showing that there's no line number printed after
|\Suppressnumber|. What did you do differently? Did you receive any warnings in your.log? Perhaps you have an older version oflistings- currently at1.4. To see whether this is the case, add\listfilesbefore\documentclassand check your.logfile after*File List*. – Werner Nov 08 '11 at 18:33 -
Shame on me; I must have done something wrong. It works. Thank you all for your help. Now the listing looks exactly as I like it. Just perfect. – user9131 Nov 08 '11 at 19:03