Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have read through the listings documentation and couldn't find an answer to the following question: How do I insert some sort of visual feedback in a listings listing when using the linerange option. It works fine on my source code files, but the only visible feedback that there is a gap in the listed code is the change in numbering. I would like some more visual feedback, e.g. a line or maybe some sort of waves that I come across in some books on programming. Any idea on how to achieve this?

share|improve this question

migrated from stackoverflow.com Sep 30 '11 at 11:29

This question came from our site for professional and enthusiast programmers.

add comment

1 Answer 1

up vote 3 down vote accepted

One way to do this is to split the listings for each line range, and place an ornament in between them. There are many options as to what to use as a separator. To illustrate, I have used an ornament from this question in between the listings and another from this question on how to insert a border at the end of the listings.

enter image description here

Here is the code:

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

%------------------------ Used for ornament between listings
% Ornament from http://tex.stackexchange.com/questions/11320/end-of-paragraph-with-ornament
\usepackage{pifont,fourier-orns}% These are needed only for the ornament 

\newcommand\crulefill[1][1ex]{\leavevmode\leaders\hrule depth \dimexpr-#1+0.4pt height #1\hfill\kern0pt}
\newcommand\ornline[2][1ex]{\trivlist\item\crulefill[#1]#2\crulefill[#1]\endtrivlist}

%------------------------ Used for ornament at end of listings
%http://tex.stackexchange.com/questions/30973/how-do-i-insert-a-border-below-text/30979#30979
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\myrule}{O{1pt} O{3pt} O{black}}{%
  \par\nobreak % don't break a page here
  \kern\the\prevdepth % don't take into account the depth of the preceding line
  \kern#2 % space before the rule
  {\color{#3}\hrule height #1 width\hsize} % the rule
  \kern#2 % space after the rule
  \nointerlineskip % no additional space after the rule
}
%------------------------

\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}
\ornline[0.6ex]{\decoone}

\lstinputlisting[style=MyListStyle,linerange={9-11},firstnumber=9]{foo.java}
\myrule[5pt][5pt][orange]
\end{document}
share|improve this answer
    
Thanks, a little late for my thesis but I will definitely use this in my master thesis. Great! –  Lennert Dec 3 '11 at 12:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.