5

I am very new to TeX.

My source code is in an external file, let's call it source.java, and I use \lstinputlisting{source.java}.

  1. How could I use only the lines 3-5 for my code listing? (and not the whole file?)

  2. How could I use only specific lines, e.g., line {1,3,7,12} in the code listing? (I would use this last example only to show the important lines while I still have a running program in source.java.)

  3. Could I do the same with the minted package?

9

2012-02-01: Updated to allow for adjustment of spacing between subsequent lines.


Here is a solution for the first two problems adapted from In listings, how to show referenced linenumbers instead of standard ascending linenumbers.

Syntax:

\ShowListingForLineNumber*[percentage]{<line number>}}{<file name>}

where:

  • * adjust space above current line (optinal)
  • [percentage] = percentage of \baselineskip to leave above current line (optional, ignored if * is not used). If not specified this is defaults to be 1.0 meaning the entire spacing above is suppressed.
  • <line number>} is the line number of which the listing is to be printed
  • <file name> is the file name of the input file

enter image description here

Notes:

Code:

\documentclass{article}
\usepackage{filecontents}
\usepackage{xcolor}
\usepackage{xparse}% to define star variant of macro
\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,
    backgroundcolor={\color{yellow}},
    breaklines=true
    }
\begin{document}
\noindent
Showing line range 3-5:
\lstinputlisting[
    style=MyListStyle,
    linerange={3-5},
    firstnumber=3,
    ]{foo.java}

\bigskip\noindent
Showing lines 1,3,7,12 (note that Line 7 is blank) with starred version between lines 1 and 3 to supress the space and the space before line 12 set to 50\% of the \verb|\baselineskip|:

\NewDocumentCommand{\ShowListingForLineNumber}{s O{1.0} m m}{
    \IfBooleanTF{#1}{\vspace{-#2\baselineskip}}{}
    \lstinputlisting[
            style=MyListStyle,
            linerange={#3-#3},
            firstnumber=#3,
            ]{#4}
}%
\ShowListingForLineNumber{1}{foo.java}
\ShowListingForLineNumber*{3}{foo.java}% supress space before
\ShowListingForLineNumber*[0.5]{7}{foo.java}% supress 50% of the space before
\ShowListingForLineNumber*{12}{foo.java}
\end{document}
  • One Problem... the solution is really great. But to show single Lines, an additional Space is added in between the Lines, e.g., between the lines 1, 3, 12 there is a blank line each. In my paper this takes quite some additional Space and doesn't look nice... – mrsteve Feb 1 '12 at 21:22
  • You can add a \vspace{-\baselineskip} before a subsequent ShowListingForLineNumber. Let me know if that works for you. – Peter Grill Feb 1 '12 at 21:44
  • @mrsteve: Updated solution to provide option for adjustment of interline spacing. – Peter Grill Feb 1 '12 at 22:27

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.