[Tex/LaTex] Background overflows when using rounded corners for listings (package: `listings`)

backgroundscolorlistingsrounded-corners

I'm using listings to list code in my document, and I want the code to be displayed in a frame with rounded corners and a grey background color. But the background overflows the frame, as if the frame was square.

Here's a minimal example:

\documentclass[a4paper, 11pt]{article}

\usepackage{color}
\usepackage{listings}

\begin{document}

\lstset{basicstyle=\footnotesize\ttfamily, xleftmargin=1em, xrightmargin=1em,
  frame=single, framesep=1em, frameround=tttt,
  backgroundcolor=\color[rgb]{0.85, 0.85, 0.85}, captionpos=b}
\begin{lstlisting}
bla bla bla
\end{lstlisting}

\end{document}

Result:
result

Any idea on how to fix this?

EDIT

I got the example working now. The problem was due to old versions of required packages.

Best Answer

Based on the link in my comment I created an environment in which you can use the lstlistings-environment:

Code

\documentclass[a4paper, 11pt]{article}

%\usepackage{color} loaded by tikz and you should use xcolor instead ...
\usepackage{listings}
\usepackage{tikz}

% set listings
\lstset{%
    basicstyle=\footnotesize\ttfamily,
%   captionpos=t,
    framesep=5em,
}

% define backgroundcolor
\definecolor{bggray}{rgb}{0.85, 0.85, 0.85}

% add frame environment
\usepackage[%
    framemethod=tikz,
    skipbelow=\topskip,
    skipabove=\topskip
]{mdframed}
\mdfsetup{%
    leftmargin=0pt,
    rightmargin=0pt,
    backgroundcolor=bggray,
    middlelinecolor=black,
    roundcorner=10
}

\usepackage{etoolbox}% >= v2.1 2011-01-03
\BeforeBeginEnvironment{lstlisting}{\begin{mdframed}\vspace{-0.7em}}
\AfterEndEnvironment{lstlisting}{\vspace{-0.5em}\end{mdframed}}

% needed for \lstcapt
\def\ifempty#1{\def\temparg{#1}\ifx\temparg\empty}

% make new caption command for listings
\usepackage{caption}
\newcommand{\lstcapt}[2][]{%
    \ifempty{#1}%
        \captionof{lstlisting}{#2}%
    \else%
        \captionof{lstlisting}[#1]{#2}%
    \fi%
    \vspace{0.75\baselineskip}%
}

% only for testing ...
\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}
\blindtext
%
\begin{lstlisting}
Foo {
    Bazzzzz }
Bar
\end{lstlisting}
\lstcapt{Test}
%
\blindtext
%
\begin{lstlisting}
Foo
\end{lstlisting}
\lstcapt[Short descrpition]{Another long description. Another long description.
    Another long description.}
%
\blindtext
%
\lstlistoflistings
\end{document}

Result

result

Limitation

This solution works only for non page breaking listings. See the answers to Test if a paragraph has a page break in it? for an idea how this could work. See Update 3

Update

I put this three lines to the above example to patch the lstlisting environment dircetly. So there’s no need to put it manually in \begin{code} … \end{code} anymore.

\usepackage{etoolbox}
\BeforeBeginEnvironment{lstlisting}{\begin{code}}
\AfterEndEnvironment{lstlisting}{\end{code}} 

Update 2

I created a new command \lstcapt[<Short>]{<Text>} to set a caption out of the frame, and added it to the above example code.

Update 3

I changed to mdframed instead of using TikZ directly to solve the page breaking problem as Marco suggested.

Update 4

Please note that this code needs etoolbox v2.1 or higer …

Update 5: Filelist

This is the result of \filelist before \documentclass

*File List*
article.cls 2007/10/19 v1.4h Standard LaTeX document class
size11.clo 2007/10/19 v1.4h Standard LaTeX file (size option)
listings.sty 2007/02/22 1.4 (Carsten Heinz)
keyval.sty 1999/03/16 v1.13 key=value parser (DPC)
lstmisc.sty 2007/02/22 1.4 (Carsten Heinz)
listings.cfg 2007/02/22 1.4 listings configuration
tikz.sty 2010/10/13 v2.10 (rcs-revision 1.76)
pgf.sty 2008/01/15 v2.10 (rcs-revision 1.12)
pgfrcs.sty 2010/10/25 v2.10 (rcs-revision 1.24)
everyshi.sty 2001/05/15 v3.00 EveryShipout Package (MS)
pgfrcs.code.tex
pgfcore.sty 2010/04/11 v2.10 (rcs-revision 1.7)
graphicx.sty 1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
graphics.sty 2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
trig.sty 1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg 2010/04/23 v1.9 graphics configuration of TeX Live
pdftex.def 2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty 2010/04/08 v1.3 Providing info/warning/message (HO)
ltxcmds.sty 2011/04/18 v1.20 LaTeX kernel commands for general use (HO)
pgfsys.sty 2010/06/30 v2.10 (rcs-revision 1.37)
pgfsys.code.tex
pgfsyssoftpath.code.tex 2008/07/18 (rcs-revision 1.7)
pgfsysprotocol.code.tex 2006/10/16 (rcs-revision 1.4)
xcolor.sty 2007/01/21 v2.11 LaTeX color extensions (UK)
color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
pgfcore.code.tex
pgfcomp-version-0-65.sty 2007/07/03 v2.10 (rcs-revision 1.7)
pgfcomp-version-1-18.sty 2007/07/23 v2.10 (rcs-revision 1.1)
pgffor.sty 2010/03/23 v2.10 (rcs-revision 1.18)
pgfkeys.sty
pgfkeys.code.tex
pgffor.code.tex
tikz.code.tex
mdframed.sty 2011/08/06 v0.7a: mdframed
kvoptions.sty 2010/12/23 v3.10 Keyval support for LaTeX options (HO)
kvsetkeys.sty 2011/04/07 v1.13 Key value parser (HO)
etexcmds.sty 2011/02/16 v1.5 Prefix for e-TeX command names (HO)
ifluatex.sty 2010/03/01 v1.3 Provides the ifluatex switch (HO)
etex.sty 1998/03/26 v2.0 eTeX basic definition package (PEB)
calc.sty 2007/08/22 v4.3 Infix arithmetic (KKT,FJ)
etoolbox.sty 2011/01/03 v2.1 e-TeX tools for LaTeX
md-frame-1.mdf 2011/08/06 v0.7a: md-frame-1
caption.sty 2011/08/18 v3.2b Customizing captions (AR)
caption3.sty 2011/08/18 v1.3b caption3 kernel (AR)
babel.sty 2008/07/08 v3.8m The Babel package
english.ldf 2005/03/30 v3.3o English support from the babel system
blindtext.sty 2009/12/29 V1.9c blindtext-Package
xspace.sty 2009/10/20 v1.13 Space after command names (DPC,MH)
supp-pdf.mkii
pdftexcmds.sty 2011/04/22 v0.16 Utilities of pdfTeX for LuaTeX (HO)
ifpdf.sty 2011/01/30 v2.3 Provides the ifpdf switch (HO)
epstopdf-base.sty 2010/02/09 v2.5 Base part for package epstopdf
grfext.sty 2010/08/19 v1.1 Managing graphics extensions (HO)
kvdefinekeys.sty 2011/04/07 v1.3 Defining keys (HO)
epstopdf-sys.cfg 2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
***********