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?

  • 1
  • Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. Also, I'd recommend you include the input and the desired output (it is not clear from your description). – Peter Grill Sep 25 '14 at 23:58
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.

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 5:45

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.