Is there a way to input particular sections of source code using \lstinputlisting
(from the listings
package) in a flexible way? It's possible to use the firstline
and lastline
options to input particular line ranges, but this isn't robust to changes to the source file.
For example, suppose that the input source file is:
int square(int x) {
return x * x;
}
int cube(int x) {
return x * x * x;
}
In my LaTeX document, I can include the cube
procedure using the command \lstinputlisting[firstline=5,lastline=7]{source.c}
.
But suppose I later decide to rewrite the cube
procedure. The input source file becomes:
int square(int x) {
return x * x;
}
int cube(int x) {
int x2;
x2 = square(x);
return x * x2;
}
Now my LaTeX document will not show the final two lines of cube
because the listing uses only lines 5-7. I could manually update the line range, but this is tedious and error-prone.
Is there a way to mark sections of source code with some kind of label so that the listing will be robust to these sorts of changes? For instance, perhaps the input source file would be something like
int square(int x) {
return x * x;
}
/**\beginlabel{lst:cube} **/
int cube(int x) {
int x2;
x2 = square(x);
return x * x2;
}
/**\endlabel{lst:cube} **/
so that I could use something like \lstinputlisting[lines=lst:cube]{source.c}
to include the section that I want and remain robust to changes.
I am open to using other packages for source code listings (e.g., minted
) if helpful.