I'm trying to create a verbatim environment with a colored background and which can span across pages (so using a colorbox is not an option). It seemed that the listings package was a good way towards it, but the background is drawn one line at a time, such that, when I view the PDF, I see annoying white-ish "stripes" between the lines as well as where the invisible (0pt) frame rule was not to be drawn:
http://a.imageshack.us/img202/9928/lststrips.png
Here's the minimal code I used to create the output shown in the image:
\documentclass{minimal}
\usepackage[pdftex]{xcolor}
\usepackage[a4paper,hmargin=6cm]{geometry}
\usepackage{listings}
\lstset{backgroundcolor=\color{gray},
frame=single, framerule=0pt, framesep=5pt}
\begin{document}
\begin{lstlisting}
if (a < b)
{
printf("A is smaller than B!\n");
}
a = b;
\end{lstlisting}
\end{document}
Is there any workaround against these 'stripes'?
A simple workaround would be to not specify a color for the listings themselves, put instead use a \colorbox
, but for that to work, you either need to use \lstinputlisting
or store the result in a box using e.g. lrbox
.
\newbox{\mybox}
\begin{lrbox}{\mybox}
\begin{minipage}{\linewidth}
\begin{lstlisting}
if (a < b)
{
printf("A is smaller than B!\n");
}
a = b;
\end{lstlisting}
\end{minipage}
\end{lrbox}
\colorbox{gray}{\usebox{\mybox}}
UPDATE: However, a more beautiful solution is to use Donald Arseneau's framed.sty, which also allows the source-code to span multiple pages.
\documentclass{minimal}
\usepackage[pdftex]{xcolor}
\usepackage[a4paper,hmargin=6cm]{geometry}
\usepackage{listings}
\usepackage{framed}
\begin{document}
\definecolor{shadecolor}{named}{gray}
\begin{shaded}
\begin{lstlisting}
if (a < b)
{
printf("A is smaller than B!\n");
}
a = b;
\end{lstlisting}
\end{shaded}
\end{document}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With