2

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}

1 Answers1

1

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.