12

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?

16

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}

9

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

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.