[Tex/LaTex] Listings code snippet has white horizontal lines across it

listings

I'm using the listings package to typeset my code examples. I've set the background color to slightly off white color and I'm noticing strange white horizontal lines across the snippet. They seem to go right between every line.

Not sure if I should include my listings settings?

Listings bug

Best Answer

The background color and the frame are drawn for every line separately. listings assumes a certain line height and such graphic errors appear if the real height is larger than that. From your image I would guess you are using a larger font for the line numbers than for the normal text. You should set numberstyle to the same or smaller size than the normal text.

In my test I get the same white lines if the numberstyle is larger than the basicstyle (left) but not if it is smaller (right) or the same size.

Comparison

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\begin{document}

\begin{minipage}{.2\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize,numberstyle=\large,backgroundcolor=\color{black!20},numbers=left,frame=lrtb]
    some code
    more code
    more code
    more code
    more code
    more code
    more code
    more code
    more code
\end{lstlisting}
\end{minipage}\hspace{2cm}%
\begin{minipage}{.2\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize,numberstyle=\tiny,backgroundcolor=\color{black!20},numbers=left,frame=lrtb]
    some code
    more code
    more code
    more code
    more code
    more code
    more code
    more code
    more code
\end{lstlisting}
\end{minipage}


\end{document}