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.

I want to emphasize some lines in a listing environment by applying a special style on them (for example, a different background color).

All custom styling seem to be content-based. I would like to be able to do something like:

\lstinputlisting[emphlines={4},emphstyle=\color{red}]{sourcecode.f}

If it is not possible with the plain listings package, what do you suggest to get this effect?

My goal is to highlight different lines in different slides of a beamer presentation, so if there is a simple way to do this, I will be very thankful.


Edit: I came up with this solution after Ulrike's idea. I'm satisfied with the result, and the redundancy is bearable for my taste. It'd be very nice if someone more knowledgeable in TeX scripting suggests some improvements.

share|improve this question
    
In case you're displaying line numbers, it's useful to add a firstnumber=last option, so that numbering sequentiality is preserved. –  Roberto Aloi Jul 27 '12 at 9:27

3 Answers 3

up vote 7 down vote accepted

The only way I know is to reinput the file with different firstline/lastline settings. Not difficult but a bit tedious:

\documentclass{article}
\usepackage{listings,color}
\begin{document}
\lstinputlisting[lastline=4,belowskip=0pt]{test-utf8.log}
\lstinputlisting[firstline=5,lastline=5,
   aboveskip=0pt,belowskip=0pt,backgroundcolor=\color{yellow}]{test-utf8.log}
\lstinputlisting[firstline=6,aboveskip=0pt]{test-utf8.log}
\end{document}
share|improve this answer
    
Maybe this can be made less tedious by using TikZ' foreach macro. I'll give it a try, thanks! –  Roberto Bonvallet Aug 24 '10 at 17:42

The simplest way is to use the semiverbatim environment from beamer, see Section 3.13 (Verbatim Text) of the manual. The disadvantage of this method is that you don’t get highlighting.

In order to have all the functionality of the listings package, you could use its fancyvrb interface with commandchars:

\documentclass{beamer}
\usepackage{fancyvrb,listings}

\begin{document}

\begin{frame}[fragile]
    \lstset{language=pascal,fancyvrb=true,basicstyle=\normalfont}
    \fvset{commandchars=\|[]}
    \begin{Verbatim}
|alert[for i:=maxint to 0 do]
begin
{ do nothing }
end;
    \end{Verbatim}
\end{frame}

\end{document}

The symbols in commandchars are replacement characters for \, { and }, properly escaped with backslashes.

I don’t know however how to get overlay specifications (like <1>) to work.

share|improve this answer
    
I didn't know about semiverbatim. I'll take a look to the manual, thanks! –  Roberto Bonvallet Aug 24 '10 at 17:51

You could save some arguments in your \emphline-command if you let LaTeX do some of calculations. linerange doesn't seem to like numexpr, but lastline and firstline works fine. E.g in your example you could use

\newcommand{\emphline}[2]{%                                                    
 \lstinputlisting[lastline=\the\numexpr#1-1\relax]{#2}            
 \lstinputlisting[linerange={#1}-{#1},style=h]{#2}                            
 \lstinputlisting[firstline=\the\numexpr#1+1\relax]{#2}                       
}                                                                              

and then

 \only<1> {\emphline{6} {potencia.f95}}             
share|improve this answer
    
I didn't know about \numexpr either. This opens a world of possibilities! –  Roberto Bonvallet Aug 25 '10 at 16:02

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.