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 wanted to tidy up a TeX document and was curious if you can use \lstinputlisting or a similar function to include a certain part of a source-code file.

Example, I have a 500 or so line source file and I want to include just lines 300-400.

Is there a way to do this?

share|improve this question

3 Answers 3

up vote 22 down vote accepted

Each "listing" environment understands the firstline and lastline keys:

\lstinputlisting[firstline=300,lastline=500]{file.cc}
share|improve this answer
    
Thanks =) I also just found this in the reference (4.4 - The printed range). But I asked because I had trouble finding it :) –  Supernovah Nov 10 '11 at 7:57
    
Personally I think linerange is more succinct –  Robert Martin Nov 15 '11 at 18:37

listings supports line ranges not only with firstline and lastline (see egregs answer) but even with linerange={<first1>-<last1>,<first2>-<last2> …}. Note, that first2 has to be greater than last1 and so on. So

\lstinputlisting[linerange={1-4,7-9}]{file.cc}

would be valid but

\lstinputlisting[linerange={7-9,1-4}]{file.cc}% Don't do this!!!

is not allowed!

For more information see section 4.4 "The printed range" at the listings manual.

share|improve this answer
1  
...but note that the line numbering using this option will be continuous - so if you want to make it obvious that you haven't printed some code , using @egreg's approach multiple times (possibly with some explanatory text along the lines of "setup routine omitted" or even just $\vdots$ and specifying firstnumber= might be better. A firstnumber=firstline option would be nice. –  Chris H Apr 10 at 9:49
    
@ChrisH: You can also use my suggestion more than once with only one range every time and add something like $\vdots$ between. –  Schweinebacke Apr 16 at 14:52
    
@ChrisH: Unfortunately, the float argument cannot be used in that case, as the two listings need to stay together. Also, I fear there are conflicts with the frames, as listings puts frames around single listings, rather than the whole set of listings that should be considered as one listing (with omissions). –  O. R. Mapper Sep 26 at 11:14
    
@O.R.Mapper, if you say so, I believe you - and I can see why it would be true. I've never floated my listings so haven't experienced this (my listings have all been in appendices and run to several pages each even with the most boring bits skipped). –  Chris H Sep 27 at 18:59

As an addendum to egreg's answer: If you are typesetting line numbers and want these numbers to reflect the physical line numbers, the firstnumber key comes at your rescue:

\lstinputlisting[numbers=left,firstnumber=300,firstline=300,lastline=500]{file.cc}
share|improve this answer
    
Yup thanks, I have found that already in the reference, but didn't read section 4.4. Do you know how I can skip line 149 of my file as it contains a string too long for the bounding box. Error "Dimension too large " ... "" –  Supernovah Nov 10 '11 at 8:09
    
@Supernovah: Use either linerange, or split your \lstinputlisting into two separate instances that skip line 149. –  Werner Nov 10 '11 at 8:12
    
The compiler still hiccups with linerange splitting over 149 -Excuse me it actually doesn't, thanks - I had a syntax error and the IDE was using an old file –  Supernovah Nov 10 '11 at 8:13

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.