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'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(){
    }
}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

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

Sample output

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}
share|improve this answer
    
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

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.