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?


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

  • 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
  • 2
    Personally I think linerange is more succinct – Robert Martin Nov 15 '11 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 – Erik Pillon Mar 14 '18 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. – Erik Pillon Mar 14 '18 at 15:45

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


would be valid but

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

is not allowed!

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

  • 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 '14 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 '14 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 '14 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 '14 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. – Schweinebacke Nov 17 '17 at 9: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:

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.