3

I want to automate source code examples in my documentation. Listing comes very close to doing this as it allows specification of start and ending line numbers:

\lstinputlisting[firstline=300,lastline=500]{file.cc}

See: Using \lstinputlisting to include a file but only certain lines or line ranges

However, source code tends to be a living document. I would love some way to markup my code with some kind of EXAMPLE_BEGIN and EXAMPLE_END type of tag in the comments and let that define the bounds for the listing.

If this isn't possible with a current package, I'm not opposed to writing one that does this. If that's the case, is there a good starting point? (i.e. Modifying Listing? Modifying Minted? Parsing with a macro?)

DiB
  • 242

1 Answers1

3

Quoted from listings documentation:.

Instead of using linerange with line numbers, one can use text markers. Each such marker consists of a , , and a . You once (or more) define prefixes and suffixes and then use the marker text instead of the line numbers.

\documentclass{article}

\usepackage{filecontents}
\usepackage{listings}

\begin{filecontents*}{livingdocument.txt}
  Here some text which will not be included
  EXAMPLE_BEGIN
  1. All these texts will be included
  2. All these texts will be included
  3. All these texts will be included
  EXAMPLE_END
  Here some text as well which will not be included
\end{filecontents*}

\begin{document}

\lstinputlisting[linerange=EXAMPLE\_BEGIN-EXAMPLE\_END,includerangemarker=true]{livingdocument.txt}

\end{document}

enter image description here

(Those shifts must be coming from the default language settings.)


For more details please see Section 6.8 Multicolumn Listings in listings documentation.

Masroor
  • 17,842