Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
5  
Please accept answers. In your previous question there are 2 answers without any response: tex.stackexchange.com/questions/33999/… –  Marco Daniel Nov 8 '11 at 12:15
add comment

1 Answer

up vote 9 down vote accepted

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.

share|improve this answer
    
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 8 '11 at 17:32
    
I suppose that you do not need the \global and why do you have all the \relax after the let statement? –  Herbert Nov 8 '11 at 17:41
    
@Herbert: The \relax is obsolete. I know this. I don't know why I haven't changed it. -- I tested without global and it works. –  Marco Daniel Nov 8 '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 8 '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 of listings - currently at 1.4. To see whether this is the case, add \listfiles before \documentclass and check your .log file after *File List*. –  Werner Nov 8 '11 at 18:33
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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