5

By default, the listings package suppresses empty lines at the end of the file, and this behavior can be switched off with the option showlines.

However, listings also suppresses empty lines at the end of each range in a linerange. Consider the following Java source file:

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,%
  commentstyle=\color{blue}\itshape}

\begin{document}

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

\end{document}

The expected output is:

public class Example implements StringHandler {

    public void handle(String s) {
        System.out.println(s);
    }

}

But instead, I get

public class Example implements StringHandler {
    public void handle(String s) {
        System.out.println(s);
    }

}

Line 2 (which is empty) is not included in the output, being at the end of a line range. (I am using listings v1.5; but v1.3 behaves similarly.)

It turns out that empty lines at the beginning of a line range (consisting of more than one line) are included. But that is of no help in this situation.

How can I force the inclusion of these embedded empty lines? This is useful to make the listing more readable.

The options showlines and emptylines do not affect this behavior.

  • Did you ever find a solution? – user2768 Oct 3 '18 at 15:46
  • 1
    @user2768: No, I never a solution. – Tom Verhoeff Jan 5 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.