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.

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:

\emphline{7}{potencia.f95}

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

\emphline{7-12}{potencia.f95}
share|improve this question
    
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
add comment

1 Answer

up vote 1 down vote accepted

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

\newcounter{linenumber}
...
\setcounter{linenumber}{#1}
\addtocounter{linenumber}{-1}
\lstinputlisting[linerange={1-\value{linenumber}}]{#2}
\lstinputlisting[linerange={#1-#1},style=highlight,firstnumber=last]{#2}
\addtocounter{linenumber}{2}
\lstinputlisting[firstline=\the\value{linenumber},firstnumber=last]{#2}

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

share|improve this answer
add comment

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.