2

So I am trying to use the listings package to show some source code. However I would like to be able to skip line numbers for certain lines, such that the do not receive any line number, but does not increment the line number either. It is very similar to this: Suppress line numbering for specific lines in listings package

except that I need to be able to suppress the first line. SOmething like this:

\begin{lstlisting}
|\Suppressnumber|First line.|\Reactivatenumber| % ONLY SKIP LINE NUMBER ON THIS LINE!
Second line. % START WITH LINE NUMBER 1 HERE
Third line.
Next line.
Next Line
\end{lstlisting}.

How do I change this command:

\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}%
}

So it works like I want?

For others with the same issue, currently I am using this solution:

\documentclass{article}
\usepackage{listings}
\lstset{
  basicstyle=\ttfamily,
  numberstyle=\small\ttfamily,
  %numberfirstline=false,
  firstnumber=0,
  numbers=left
}
\makeatletter
\def\lst@PlaceNumber{\ifnum\value{lstnumber}=0\else
  \llap{\normalfont\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}\fi}
\makeatother
\begin{document}
\begin{lstlisting}
INPUT: c, m
rest of code...
\end{lstlisting}
\end{document}
2

The commands \Suppressnumber on \Reactivatenumber from the answer you mentioned can be used and no change is needed. You just have to call \Suppressnumber before the \begin{lstlisting}.

Example:

\Suppressnumber
\begin{lstlisting}
    First line.|\Reactivatenumber| % NO ON THIS LINE!
    Second line. % START WITH LINE NUMBER 1 HERE
    Third line.
    Next line.
    Next Line
\end{lstlisting}

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.