# Auto include specific part of source files

I'm creating a lot of code to our small projects (in computer science), and finding myself copy/pasting my comments into the document.

Would it be possible to make a reference point in the code file to include that specific part to a \listinputlisting paragraph?

# example.java


// #Identifyer1#
// This is our class [...]
// #Identifyer1#
public class Anyclass{

// #Identifyer2#
// the constructor is here...
// #Identifyer2#

public Anyclass(){
}
}

-

The linerange option of \lstlistinginput allows you to specify arbitrary start and stop text (appropriately escaped). So here is one working version:

\documentclass{article}

\usepackage{listings}

\begin{document}

\lstinputlisting[linerange=//\ Begin\ Identifier1-//\ End\ Identifier1]{a.jar}

\end{document}


with a.jar containing

// Begin Identifier0
// #Identifier0#
// This is our class [...]
// #Identifier0#
public class Anyclass{

// #Identifier0#
// the constructor is here...

}
// End Identifier0

// Begin Identifier1
// #Identifier1#
// This is our class [...]
// #Identifier1#
public class Anyclass{

// #Identifier1#
// the constructor is here...
// #Identifier2#

public Anyclass(){
}
}
// End Identifier1

// Begin Identifier3
// #Identifier3#
// This is our class [...]
// #Identifier3#
public class Anyclass{

// #Identifier3#
// the constructor is here...
// #Identifier2#

public Anyclass(){
}
}
// End Identifier3


produces

Note that I have added comment lines that are clearly unique for this purpose. Also note the escaping of the space characters.

If you do not want the start and stop text included in the output you can add the option includerangemarker=false to the \lstinputlisting call or set this option globally via

\lstset{includerangemarker=false}

-
Thank you - That was exactly what I needed. Just one small question, is there a simple way of not including the Identifier names in the TeX document ? –  HJess Dec 20 '12 at 16:56
I have now updated my answer to explain how to exclude them. –  Andrew Swann Dec 20 '12 at 17:11
This is so great ! - Thank you for sharing this knowledge with others. –  HJess Dec 20 '12 at 17:17