87

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?

3 Answers 3

97

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

\lstinputlisting[firstline=300,lastline=500]{file.cc}
4
  • 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, 2011 at 7:57
  • 2
    Personally I think linerange is more succinct Nov 15, 2011 at 18:37
  • 1
    Only as an expansion of egreg answer, I would add that using the matlab-prettifier package works also pretty well. It is intended for Matlab (and compatible) language only, but the command \lstinputlisting[label={code:label},caption={Caption},firstline=100, lastline=300]{Matlab_Code/file.m} works greatly Mar 14, 2018 at 15:35
  • As a remark, I would suggest that also the option firstnumber=100 is really nice; it allows you to keep track of the code you cutted. Mar 14, 2018 at 15:45
53

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.3.3 "The printed range" at the listings manual.

6
  • 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, 2014 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. Apr 16, 2014 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). Sep 26, 2014 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, 2014 at 18:59
  • 1
    @yildizabdullah AFAIK the package does not support this. So in such a case you have to use multiple lstlisting environments and put the \dots or \vdots between them yourself. Nov 17, 2017 at 9:59
20

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}
3
  • 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, 2011 at 8:09
  • @Supernovah: Use either linerange, or split your \lstinputlisting into two separate instances that skip line 149.
    – Werner
    Nov 10, 2011 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, 2011 at 8:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .