I've put here one adaptation for "no-beamer" documents of this code. Thanks a lot for the indications given below by Daniel.

% Sources : 
%   1) https://tex.stackexchange.com/questions/18969/creating-a-zebra-effect-using-listings
%   2) https://tex.stackexchange.com/questions/24046/listing-zebra-effect-and-broken-lines
%   3) https://tex.stackexchange.com/questions/30877/highlighting-line-ranges-in-a-lstlisting-partial-solution-but-expansion-issues/31267#31267




% Define backgroundcolor

    \definecolor{bggray}{rgb}{0.85, 0.85, 0.85}
        leftmargin = 20pt,
        rightmargin = 20pt,
        backgroundcolor = bggray,
        middlelinecolor = black,
        roundcorner = 15

% \btIfInRange{number}{range list}{TRUE}{FALSE}
% Test if int number <number> is element of a (comma separated) list of ranges
% (such as: {1,3-5,7,10-12,14}) and processes <TRUE> or <FALSE> respectively

            \foreach \range in \bt@rangelist {%
                \pgfmathtruncatemacro\result{ ( #1 >= \bt@rangea) && (#1 <= \bt@rangeb) }%


                \bt@rangeb=100000%   \maxdimen is too large for pgfmath

% \btLstHL{range list}

% \btInputEmph[listing options]{range list}{file name}
            }% \only

% Patch line number key to call line background macro
                \kern\linewidth \kern\lst@numbersep
                \PackageError{Listings}{Numbers #1 unknown}\@ehc%

% New keys

% Line Background macro
                    \color{-.}% By default use the opposite (`-`) of the current color (`.`) as background



    language = C,
    linebackgroundcolor = {\btLstHL{4-6,9}},
* Prints Hello World.
#include <stdio.h>
int main(void) {
    printf("Hello World!");  
    return 0;


1 Answer 1


The \btIfInRange macro depends on the pgffor package. Just add it to the list of packages you load and the code compiles just fine.

Note that the implementation you use has the drawback that all nonselected lines also get a background color (which is white in your case, so you do not really see it). However, as soon as you put a listing in some \colorbox or choose another color for the main font, the result may look ugly. In my answer to the Highlighting line ranges in a lstlisting: partial solution, but expansion issues I have presented a workaround that overcomes this limitation.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .