3

I need to change background color for some lines inside a listings block. This is an example:

\documentclass{article}
\usepackage{listings}             % Include the listings-package
\usepackage{xcolor}

\newcommand{\add}{\makebox[0pt]{\color{green}\rule[-1ex]{\paperwidth}{3ex}}} 

\begin{document}

\lstset{ %
  escapechar=|,
  language=Python                       % the language of the code
}

\begin{lstlisting}

#!/usr/bin/env python

import sys
import os

class Foo:
    def __init__(self):
        x = 8
        x = 9

        |\add|# some additions
        |\add|x = 10
        |\add|if True:
        |\add|    x = 11

        x = 20
        x = 21

\end{lstlisting}
\end{document}

the problem is that instead of a single green block, it appears a "cebra" pattern with one green block for each line:

enter image description here

Some suggestions about how to fix this issue?

(I know there are several similar questions, but none of the found answers seems applicable).

Thanks.

1

Try the fvextra package (fvextra – Extensions and patches for fancyvrb), specifically, the highlightlines feature. Note that you may also have to use the line numbering option in order to deduce which are the lines that you want to highlight. Of course, you can switch this option off afterwards.

Here is your MWE (mind the capital V in Verbatim):

\documentclass{article}
\usepackage{fvextra}
\usepackage{xcolor}

\begin{document}    
\begin{Verbatim}[numbers=left, highlightlines=12-15]

#!/usr/bin/env python

import sys
import os

class Foo:
def __init__(self):
x = 8
x = 9

# some additions
x = 10
if True:
  x = 11

x = 20
x = 21

\end{Verbatim}
\end{document}

which produces

PS: for compatible (Python) syntax colouring you might want to try the pygments package from the same author. It is basically a Python script that can also output LaTeX code. Many programing languages are supported.

1

Based on his answer to Creating a zebra effect using listings, Martin Scharrer wrote lstlinebgrd package. It helps to control background lines aspect for lstlistings environments.

In this particular case, the option linebackgroundcolor is enough.

\documentclass{article}
\usepackage{listings}             % Include the listings-package
\usepackage{xcolor}

\usepackage{lstlinebgrd}

\begin{document}

\lstset{ %
  escapechar=|,
  language=Python                       % the language of the code
}

\begin{lstlisting}[
        linebackgroundcolor={%
        \ifnum\value{lstnumber}>11
            \ifnum\value{lstnumber}<16
                \color{green!35}
            \fi
        \fi},
]

#!/usr/bin/env python

import sys
import os

class Foo:
    def __init__(self):
        x = 8
        x = 9

        # some additions
        x = 10
        if True:
            x = 11

        x = 20
        x = 21

\end{lstlisting}
\end{document}

enter image description here

  • interesting approach. But the original code is the result of apply a "diff" over the two sources. Control the block of lines using its line number is not practical. – pasaba por aqui May 24 '17 at 15:23
  • @pasabaporaqui Hasta ahí no llego ;-) – Ignasi May 24 '17 at 15:29
  • Thanks for your help. Another question has been created, this time including the problem of overstrike and showing more tests done. Could be this one reaches the objective (I can not delete this one because there are already answers). – pasaba por aqui May 24 '17 at 15:35

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.