1

Hello everybody,

my question is very similar to https://stackoverflow.com/questions/2178301/linenumbering-with-listings-package-in-latex but I am trying to do the same with line range markers. The problem is the following: The line numbers become 2,6,11,16 etc. when using includerangemarker=false

Here a minimum working example:

\documentclass{scrreprt}
\usepackage{listings}

\lstset{
    rangebeginprefix=//\ begin:\ ,
    rangeendprefix=//\ end:\ ,
    includerangemarker=false, % this causes the problem
    numbers=left,
    stepnumber=5,    
    firstnumber=1,
    numberfirstline=true
}


\begin{document}                                                        
    \lstinputlisting[linerange=main-main]{./HelloWorld.java}
\end{document}

and HelloWorld.java:

public class HelloWorld {
    // begin: main
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");      
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
    }
    // end: main
}

How can I exclude the range markers and still get the lines 1,5,10,15 etc. numbered?

1

This seems to work but no guaranties that it doesn't break something else…

\lst@DefRangeB@ is one of a very few places in listings.sty where \lst@ifincluderangemarker ... \else ... \fi is used. The code below adds \global\advance\c@lstnumber\m@ne to the false branch, i.e. it reduces the line number by one if includerangemarker is false.

\documentclass{article}
\usepackage{listings}

\usepackage{etoolbox}
\makeatletter
\patchcmd\lst@DefRangeB@
  {\lst@InitLstNumber\fi}% search
  {\lst@InitLstNumber\global\advance\c@lstnumber\m@ne\fi}% replace
  {}% success
  {\err}% failure
\makeatother

\lstset{
  rangebeginprefix = //\ begin:\ ,
  rangeendprefix   = //\ end:\ ,
  includerangemarker = false , % this causes the problem
  numbers     = left ,
  stepnumber  = 5 , 
  firstnumber = 1 ,
  numberfirstline = true
}

% just to keep the example in one file:
\usepackage{filecontents}
\begin{filecontents*}{HelloWorld.java}
public class HelloWorld {
    // begin: main
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");      
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
    }
    // end: main
}
\end{filecontents*}

\begin{document}                                                        

\lstinputlisting[basicstyle=\ttfamily,linerange=main-main]{HelloWorld.java}

\end{document}

enter image description here

| improve this answer | |
  • I tested it in a document with multiple \lstinline-commands and some normal listing-environments and it seems to break nothing. Could you please explain your patch a bit to avoid mistakes when using it? – Bambino Feb 1 '17 at 8:33

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.