Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
I added a picture to make the problem visible. Se also tex.stackexchange.com/questions/8925/… which seems to be nearly the same question … –  Tobi Sep 6 '11 at 14:13
    
@Tobi: Thanks for adding the picture. –  gablin Sep 7 '11 at 9:34

2 Answers 2

up vote 8 down vote accepted

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[%
    style=1,
    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
***********
share|improve this answer
    
Unfortunately, I can't get your example to compile. It says: ! Undefined control sequence. <recently read> \BeforeBeginEnvironment l.38 \BeforeBeginEnvironment {lstlisting}{\begin{code}} –  gablin Sep 7 '11 at 10:33
    
Hm, I can't get it working satisfactory. First, the listing is indented about an inch or so within the shaded box. Second, the caption is also included into the box (this I do not want). –  gablin Sep 7 '11 at 10:58
    
@gablin: Sorry for forgetting the caption, se my 2nd update. I can’t follow your second problem with indenting. Could you provide an example (and a picture) and add it to your original question please? –  Tobi Sep 7 '11 at 16:02
    
Done. I don't understand why it doesn't work for me. And I keep getting this error when I try using etoolbox. Maybe it hasn't been probably installed or something (although the package does exist and comes bundled with TeXLive). –  gablin Sep 7 '11 at 19:28
    
@Tobi: Sorry for the counterexample. –  Marco Daniel Sep 7 '11 at 22:11

You can also use the package mdframed which allows page breaking:

\documentclass[a4paper, 11pt]{article}


\usepackage{listings}
\lstset{%
    basicstyle=\footnotesize\ttfamily,
    abovecaptionskip=0pt,belowcaptionskip =0pt,
    framextopmargin=-\topsep, 
}

% define backgroundcolor

\usepackage[style=1,skipbelow=\topskip,skipabove=\topskip]{mdframed}
\definecolor{bggray}{rgb}{0.85, 0.85, 0.85}
\mdfsetup{leftmargin=20pt,rightmargin=20pt,backgroundcolor=bggray,middlelinecolor=black,roundcorner=15}
\BeforeBeginEnvironment{lstlisting}{\begin{mdframed}\vskip-.5\baselineskip}
\AfterEndEnvironment{lstlisting}{\end{mdframed}}

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

\begin{document}
\blindtext
%
\begin{lstlisting}[caption=foo]
Foo {
    Bazzzzz }
Bar
\end{lstlisting}
%\lstcapt{Test}
%
\blindtext
%
\begin{lstlisting}
Foo

Foo {
    Bazzzzz }
Bar


Foo {
    Bazzzzz }
Bar


Foo {
    Bazzzzz }
Bar

Foo {
    Bazzzzz }
Bar

Foo {
    Bazzzzz }
Bar

Foo {
    Bazzzzz }
Bar
\end{lstlisting}
\blindtext
%
\lstlistoflistings
\end{document}

enter image description here

share|improve this answer
    
+1 for solving the page breaking problem in an easy way. I added your solution to my code, hope that’s ok with you? –  Tobi Sep 7 '11 at 23:03
    
@Tobi: Now problem. But I will add this code to my documentation. –  Marco Daniel Sep 7 '11 at 23:07
    
So mdframed is your package? And it uses TikZ internally? –  Tobi Sep 7 '11 at 23:14
    
Checkt it on CTAN myself. Seems to be a useful package :-) Greetings from Osnabrück ;-) –  Tobi Sep 7 '11 at 23:19
    
@Tobi: "Die Nordlichter" ;-) –  Marco Daniel Sep 7 '11 at 23:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.