views:

37

answers:

2

Hello,

I am writing a program (in C, but I suppose that's not so relevant) in connection with a little documentary material in LaTeX. I want the documentary material to contain code snippets from my original code.

In order to include source code and keep it up to date, I do the following in my document:

\lstinputlisting[firstline=200, lastline=210]{../src/source.c)

This loads automatically the lines 200 to 210 (which contain e.g. a function) from ../src/source.c into my document.

However, if I add some lines before line 200, this means that line 200 "wanders down some lines", so I have to adjust this in order to get my original function.

So here's my question: Does anybody know about a possibility how to dynamically tell lstinputlisting (or any adequate substitute) to tell which lines to take?

I imagine something like the following: I add special comments to my C source code that will be recognized by lstinputlisting, e.g.

/// lstinputlisting "myfunc" BEGIN
int myFunction(int x){
  return x+2;
}
/// lstinputlisting "myfunc" END

Then, lstlisting scans the file and just includes the lines between the BEGIN and the END things.

A: 

Would not be easier to use #include in C?

It is not perfect, but good enough, solution.

Grzegorz Gierlik
I'm not sure, but I think you misunderstood me. I want to include C code into LaTeX.
phimuemue
I meant to keep each fragment of C code you what to include into LaTeX in separate file (this solve line range problem), which will be included into C file with `#include`.
Grzegorz Gierlik
A: 

The only reasonable way of making this happen that I can think of off the top of my head, is to create a makefile and have that be responsible for producing the correct output.

Assuming sourcefile.c is in ./src and LaTeX files are in ./tex then ./tex/Makefile could look something like this:

doc.tex: sourcefile.grep
        <command to compile doc.tex>
sourcefile.grep: 
        <command to grep/whatever to dump from 
        ../src/sourcefile.c to ./tex/sourcefile.grep>

And the lstlistings in doc.tex would then point to ./src/sourcefile.grep

kahen
Yep, thought of this too, but that looked like an overkill to me. Saw your edit: `grep` might be an option.
phimuemue