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 have code that i have included into my document using the texcode below

\begin{minipage}{.48\textwidth}
\centering
\lstinputlisting[numbers=left,xleftmargin=2em,frame=single,framexleftmargin=2em, breaklines]{codesamples/example2.py}
\caption{Modified \soc\ for R, R' \label{R'}}
\end{minipage}

I would like to highlight some of the lines in the output. Is this possible?

share|improve this question
    
I think the following will help: Showing only specific lines using the listings package. The reason is because you're including your code as an external file and may only want to highlight certain lines. –  Werner Jun 4 '12 at 18:58
    
@thanks, but this wont work for me, I would like to show all the lines –  user739807 Jun 4 '12 at 19:02
1  
There is also another interesting approach: see this answer from Daniel. –  Claudio Fiandrino Jun 4 '12 at 19:15
add comment

2 Answers

up vote 10 down vote accepted

You can use the package lstlinebgrd which extends the package listings.

Here a simple example which will highlight the third line:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{example.tex}
\documentclass{article}
\begin{document}
Hello World!
\end{document}
\end{filecontents*}

\usepackage{listings}
\usepackage{lstlinebgrd}

\begin{document}
\begin{minipage}{.48\textwidth}
\centering
\lstinputlisting[numbers=left,xleftmargin=2em,frame=single,framexleftmargin=2em, breaklines,
linebackgroundcolor={\ifnum\value{lstnumber}=3\color{green}\fi}]{example.tex}
\end{minipage}

\end{document}


Update

To specify the highlighted lines I provided in the example below a macro \lstcolorlines which can be used as an argument of the option linebackgroundcolor. The function \lstcolorlines is defined with the user interface xparse and the modul clist provided by expl3. The Syntax of the command ist:

\lstcolorlines[optional argument: color]{mandatory argumente: line numbers}

The example is:

\documentclass{article}
\usepackage{expl3,xparse}

\ExplSyntaxOn
\NewDocumentCommand \lstcolorlines { O{green} m }
{
 \clist_if_in:nVT { #2 } { \the\value{lstnumber} }{ \color{#1} }
}
\ExplSyntaxOff

\usepackage{filecontents}
\begin{filecontents*}{example.tex}
\documentclass{article}
\begin{document}
Hello World!

Hello World!

Hello World!

\verb+1+
\end{document}
\end{filecontents*}

\usepackage{listings}
\usepackage{lstlinebgrd}

\begin{document}
\begin{minipage}{.48\textwidth}
\centering
\lstinputlisting[numbers=left,xleftmargin=2em,frame=single,framexleftmargin=2em, breaklines,
linebackgroundcolor={\lstcolorlines{2,9}}]{example.tex}
\end{minipage}


\begin{minipage}{.48\textwidth}
\centering
\lstinputlisting[numbers=left,xleftmargin=2em,frame=single,framexleftmargin=2em, breaklines,
linebackgroundcolor={\lstcolorlines[yellow]{2,10}}]{example.tex}
\end{minipage}

\end{document}

share|improve this answer
    
Thanks This should work! –  user739807 Jun 4 '12 at 20:25
add comment

Marco has already pointed to Martin Scharrer's lstlinebgrd package, which I personally use for this purpose. However, the package is still pretty new and if you want or need to refrain from using another package, you could also do this "by hand" (as I did in the past).

The idea is to include the listing in three steps: (1) the part before the highlighted line, (2) the highligted line, and (3) the rest. For (2) you additionally set a background color with backgroundcolor=<color command>:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{example.c}
int main() {
  printf("Hello World!\n");
  return 0;
}
\end{filecontents*}

\usepackage{xcolor,listings}

\begin{document}
\fbox{
  \begin{minipage}{.48\textwidth}
    \centering
    \lstset{numbers=left,xleftmargin=2em,breaklines,language=C, aboveskip=0pt,belowskip=0pt}
    \lstinputlisting[lastline=1]{example.c}
    \lstinputlisting[backgroundcolor=\color{orange!30},firstline=2,lastline=2, firstnumber=2]{example.c}
    \lstinputlisting[firstline=3, firstnumber=3]{example.c}
  \end{minipage}
}
\end{document}

enter image description here

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.