This code snippet provides a way to highlight lines in a listings environment. If you consider the following line, though:

\emphline{1-6} {7} {8-15} {potencia.f95}

You'll notice how the #1 and the #3 parameters could be deducted from #2. To do so, one would need to:

  • know the total number of lines of the potencia.f95 file;
  • perform subtractions and additions in LaTeX

Would it be possible to do the above, translating the command into one which could be used as:


Or even, if one would like to highlight an entire section:

  • To highlight lines of code you can adopt an approach like Hightlight an XML code beamer or also Highlight text in code listing while also keeping syntax highlighting. – Claudio Fiandrino Jul 27 '12 at 9:56
  • That requires to edit the source code to include style information. I need to preserve the original source code, which is embedded from an external file. – Roberto Aloi Jul 27 '12 at 10:01
  • I see your point. My consideration has been done taking into account that in a presentation it's not possible to show too many lines of code, therefore the cost of editing more or less a snippet of 20 lines is not so high. – Claudio Fiandrino Jul 27 '12 at 10:11
  • I'm not using this in a beamer presentation. I'm just re-using the idea in a generic doc, when I need to include pages of code. – Roberto Aloi Jul 27 '12 at 10:17

Ok, I came out with the following, simple, solution. Basically, I use a counter to track the line number:


Please note how you need to use \the when specifying the firstline. This is not needed in the linerange case.

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.