# How to work around a bug in listings package for parsing (Java) comments

When using the listings package (v1.5e; though v1.3 exhibits the same behavior) to put some Java source code on slides, I run into the following weird problem (bug?). I minimized the files to help isolate the problem, so forget about what this Java code is doing.

The Java source file Example.java contains:

public class Example implements StringHandler {

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

}


And this is the LaTeX source file test.tex:

\documentclass[a4paper,landscape]{slides}
\usepackage{color,listings,courier}

\lstset{language=Java,%
basicstyle=\ttfamily,%
numbers=left,%
numberstyle=\tiny,%

\begin{document}

% Suppress javadoc tag, and @Override (to avoid cluttering the slide)
\begin{slide}
\lstinputlisting[linerange={1-4,7-7,9-13}]{Example.java}
\end{slide}

\end{document}


As you can see from the linerange, the goal is to get this on the slide:

public class Example implements StringHandler {

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

}


public class Example implements StringHandler {

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

}


The @Override is hard to get rid of. It will disappear if the javadoc is confined to a single line (without the tag): /** Prints the given string. */. But I don't want to modify the given source files.

Did I misconfigure something?

Is there any workaround for this?

It seems related to the parsing of the source file by the listings package, and in particular how it recognizes the end of a comment.

• Welcome to TeX.SX! – Andrew Swann Mar 16 '15 at 15:21
• That is a rather curios behavior (v 1.5b). I leave this comment here to remind myself to check on this question later... – Pouya Apr 15 '15 at 9:34

The listings documentation describes the linerange option as follows:

linerange={⟨first1⟩-⟨last1⟩,⟨first2⟩-⟨last2⟩, and so on}

can be used on individual listings only. The given line ranges of the listing are displayed. The intervals must be sorted and must not intersect.

The range you use,

linerange={1-4,7-7,9-13}


is valid on both counts: the intervals are sorted and do not intersect. However, the @Override line that keeps showing up when it shouldn't is not the behaviour advertised by the doc. Therefore, this can only be described as a bug. You should notify the maintainer of the listings package about it.

• I did notify the maintainer, but never got a response. – Tom Verhoeff Apr 16 '15 at 17:59