5

I need help for lstlistings. I have a code that start with:

INPUT: c, m
rest of code...

I want that the first line doesn't view the number line. This is my lstset:

\lstset{language=Java,
 numberstyle=\scriptsize,
 numbers=left,
 numbersep=5pt,
 numberblanklines=false,
 captionpos=b,
 basicstyle=\small\sffamily,
        columns=fullflexible,
        xleftmargin=16pt,
        frame=leftline,
 breaklines=true,
 tabsize=2,
 prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
        escapeinside={$}{$}
}

Someone can help me please?

2 Answers2

5

While listings provides the key-value numberfirstline (set to false by default), it doesn't seem to deliver on the promise of not numbering the first line. As such, the following is work-around: Start "numbering" from 0 and check when printing the number whether it should be printed or not using a conditional.

enter image description here

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

If placement of the number is to the right (using numbers=right), then the following redefinition of \lst@PlaceNumber is required:

\makeatletter
\def\lst@PlaceNumber{\ifnum\value{lstnumber}=0\else
  \rlap{\normalfont\kern\linewidth \kern\lst@numbersep\lst@numberstyle{\thelstnumber}}\fi}
\makeatother

Note that these redefinitions of \lst@PlaceNumber should occur after setting the numbers option.

Werner
  • 603,163
3

Add firstline=2 in the \lstset.

\lstset{language=Java,
 firstline=2,
 numberstyle=\scriptsize,
 numbers=left,
 numbersep=5pt,
 numberblanklines=false,
 captionpos=b,
 basicstyle=\small\sffamily,
        columns=fullflexible,
        xleftmargin=16pt,
        frame=leftline,
 breaklines=true,
 tabsize=2,
 prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
        escapeinside={$}{$}
}
  • This will ignore the first line entirely, while the OP seems to be interested in just having it not numbered. – Werner Sep 26 '14 at 05:45