9

I'm just writing on my master thesis and have a (specific) problem with lstlisting package in LaTeX.

I'm referring to the Random.class-file in Java and only want to print some specific line-ranges, which is working fine with following lines for Latex:

\lstinputlisting[linerange={62-62,78-81,475-479},breaklines=true,language=Java, caption=Excerpt of Source Code of \texttt{Random}-class, label=list:excerptRandomCode, basicstyle=\ttfamily\tiny, captionpos=b,numbers=left,tabsize=2,float=t]{RandomClass.java}

The only problem is now, that this doesn't print the actual line numbers. It's starts with line number 1 and stops with line number 10 (thus as many lines I refer to).

How can I say, that he also prints the lines as I mentioned in the linerange? And it would be nice, if I could add one empty line without any reference between my three excerpts of the code.

Caramdir
  • 89,023
  • 26
  • 255
  • 291
Markus G.
  • 223
  • 2
  • 5

1 Answers1

8

The way to do it is to split into separate lstinputlisting commands and use firstnumber to set the line number of the first line for the given linerange. Here is an example of showing lines 2-6 and 9-11. This also provides you with an empty line in between. It should be possible to write a macro that generates the required lstinputlisting commands given a list of line ranges, but that should probably be a separate question as it is more related to macro processing than listings:

enter image description here

\documentclass{article}
\usepackage{filecontents}
\usepackage{listings}

\begin{filecontents*}{foo.java}
 public int nextInt(int n) {
     if (n<=0)
        throw new IllegalArgumentException("n must be positive");

     if ((n & -n) == n)  // i.e., n is a power of 2
         return (int)((n * (long)next(31)) >> 31);

     int bits, val;
     do {
         bits = next(31);
         val = bits % n;
     } while(bits - val + (n-1) < 0);
     return val;
 }
\end{filecontents*}


\lstdefinestyle{MyListStyle} {
    numbers=left,
    language=Java
    }
\begin{document}

\lstinputlisting[
    style=MyListStyle,
    linerange={2-6},
    firstnumber=2,
    ]{foo.java}

\lstinputlisting[
    style=MyListStyle,
    linerange={9-11},
    firstnumber=9,
    ]{foo.java}
\end{document}
Caramdir
  • 89,023
  • 26
  • 255
  • 291
Peter Grill
  • 223,288
  • This would be great of one could automate the sequence of linerange={...} ranges into a sequence of lstlistings. – Werner Oct 05 '11 at 17:02
  • This was also an idea I had, but I want to refer to his as a whole listing and not to three separate listings (that's why I labelled it with label=list:excerptRandomCode). I think there will be a problem, if I write this label to each listing. Or is there a way to make a \begin{caption?} ... \caption{text}\label{WholeListing}\end{caption?} around this list and refer then to the whole surrounding? –  Oct 05 '11 at 18:30
  • Wouldn't just captioning the last one work? – Peter Grill Oct 05 '11 at 18:50
  • That would work, but now it happens that one of the listings appears on a different page. –  Oct 05 '11 at 19:13
  • Perhaps putting the listings in a float environment? – Peter Grill Oct 05 '11 at 19:15
  • This is like \begin{float}[ht] \end{float}? –  Oct 05 '11 at 19:21
  • @Markus: You could put the set of listings in a minipage: \begin{minipage}{\textwidth} \lstinputlisting[..]{foo} \lstinputlisting[..]{foo} ... \end{minipage}. The minipage will not break across pages, therefore keeping it's contents together. – Werner Oct 05 '11 at 23:10
  • Thx Peter and Werner. I solved it with Peters Idea to split the code and found the solution that Werner suggested, even If I cannot "float" the code to top or bottom of the page. But now it looks perfect. – Markus G. Oct 06 '11 at 03:12