2

I know I can include a portion of a code file like so:

\lstinputlisting[language=XML, firstline=37, lastline=45]{source_filename.xml}

But the line numbers will change if I modify the code file. Is it possible to use references to specify the first and last lines? Basically I'm envisioning something like this:

\lstinputlisting[language=XML, firstline=\ref{tag-start}, lastline=\ref{tag-end}, escapeinside={@}{@}]{source_filename.xml}

With the portion of interest in the source_filename.xml file looking something like this:

<MyTag><!-- @\label{tag-start}@ -->
  ...
</MyTag><!-- @\label{tag-end}@ -->
c32hedge
  • 143

1 Answers1

2

Based on @Jubobs comment, I ended up with this:

LaTeX file

\lstset{
  rangeprefix=<\!--\ ,
  rangesuffix=\ -->,
  includerangemarker=false
}

\lstinputlisting[language=XML, linerange=tag\-start-tag\-end]{source_filename.xml}

source_filename.xml

<!-- tag-start -->
<MyTag>
  ...
</MyTag>
<!-- tag-end -->

This worked a trick, although I'll probably change my markers to not include hyphens.

c32hedge
  • 143
  • XML provides a Processing Instruction in the form so I am using and (and lots of other values) where I define rangeprefix=<? and rangesuffix as ?> so that I don't have to use comments, which I keep for doing actual comments in the XML. However, I am having trouble using PIs in large scripts I am documenting (see separate question). – Peter Flynn Nov 24 '18 at 23:42