2

I've just started using LaTeX and am using the Listings package to include external source code. I would like though to only include certain parts of the source code. I know that certain parts of the file can be included by specifying

[firstline=5, lastline=165]

etc. However the files in question might be modified often, and it would be very inconvenient to have to change the line numbers every time. Is it possible to somehow include parts of the file between some sort of delimeter? E.g.

public class HelloWord {

    public static void main(String[] args) {
        //{Listings:Hello World}
            System.out.println("Hello World");
        //{/Listings:Hello World}
    }

}

And then somehow only include the line

System.out.println("Hello World");

in the LaTeX document.

Even better, would it be possible to have multiple such pairs of delimiters within a file, and then include different pieces of the file in different parts of the document? e.g.

\lstinputlisting[section='Hello World']{Code.java}
...
\lstinputlisting[section='Geometry']{Code.java}

I am fine to switch to a different package if needed. Thank you for any assistance!

Daniel
  • 121

0 Answers0