4

In the listings package v1.3, when including lines from a file, the numbering is that from the file, even if lines are omitted (absolute numbers). But in v1.5, the numbering ignores the location in the file, and uses relative numbers relative to the listing.

How can you get v1.3 behavior in v1.5? (See below for why you might want this.)

As an example, consider the Java source code in file Example.java:

 public class Example implements StringHandler {

    /** 
     * Prints the given string.
     *   
     * @param s  the given string
     */  
    @Override
    public void handle(String s) {
        System.out.println(s);
    }

}

The following LaTeX code will suppress the javadoc tag:

\lstinputlisting[numbers=left,linerange={1-4,7-13}]{Example.java}

In v1.3 it produces:

 1 public class Example implements StringHandler {
 2 
 3     /** 
 4      * Prints the given string.
 7      */  
 8     @Override
 9     public void handle(String s) {
10         System.out.println(s);
11     }
12 
13 }

But in v1.5 it produces:

 1 public class Example implements StringHandler {
 2 
 3     /** 
 4      * Prints the given string.
 5      */  
 6     @Override
 7     public void handle(String s) {
 8         System.out.println(s);
 9     }
10 
11 }

Showing the actual line numbers where the lines occur in the source file helps readers to find them there. In the example above, this is not an issue. But imagine the listing of snippets from files with hundreds of lines of code.

2 Answers 2

1

Here we go hacking. I do it this way because \lst@lineno is half unpredictable in my brief testing.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{\jobname-Example.java}
public class Example implements StringHandler {

    /** 
     * Prints the given string.
     *   
     * @param s  the given string
     */  
    @Override
    public void handle(String s) {
        System.out.println(s);
    }

}
\end{filecontents*}
\usepackage{listings}

\makeatletter
\def\lsthk@EveryPar
    {% \show\lst@firstline
     % \show\lst@advancelstnum
     \ifnum\c@lstnumber<\lst@firstline
           \global\c@lstnumber\numexpr-\lst@advancelstnum+\lst@firstline
     % \lst@firstline contains ending \relax !!??!!??!!, so we use it
     % \lst@advancelstnum is \@ne per defaul
     \fi
     \global\advance\c@lstnumber\lst@advancelstnum
     \global\advance\c@lstnumber\m@ne \refstepcounter{lstnumber}%
     \lst@SkipOrPrintLabel}%
\makeatother    
\begin{document}
\lstinputlisting[numbers=left,linerange={1-4,7-13}]{\jobname-Example.java}
\end{document}

enter image description here

0

While I couldn't find the exact solution to your problem, maybe using the keyword 'firstnumber' can be a workaround:

\lstinputlisting[numbers=left, linerange={1-4}, firstnumber=1]{Example.java}    
\lstinputlisting[numbers=left, linerange={7-13}, firstnumber=7]{Example.java}
0

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.