10

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.

gablin
  • 17,006
  • I added a picture to make the problem visible. Se also http://tex.stackexchange.com/questions/8925/can-i-get-a-shadowbox-with-rounded-corners-using-listings-package which seems to be nearly the same question … – Tobi Sep 06 '11 at 14:13
  • Could you specify which are the required packages? According to MiKTeX, all my packages are up to date, but I still have the same issue. In the listings document I could only find a mention of keyval from the the graphics package, which is up to date. – Breno Jan 19 '22 at 14:38
  • Oh. Are the "old versions of required pacakages" reffering to the accepted answer? I.e. etoolbox? – Breno Jan 19 '22 at 14:42

2 Answers2

10

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
***********
Tobi
  • 56,353
  • Unfortunately, I can't get your example to compile. It says: ! Undefined control sequence. \BeforeBeginEnvironment

    l.38 \BeforeBeginEnvironment {lstlisting}{\begin{code}}

    – gablin Sep 07 '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 07 '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 07 '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 07 '11 at 19:28
  • @Tobi: Sorry for the counterexample. – Marco Daniel Sep 07 '11 at 22:11
  • @gablin: 1. The problem with indenting can be caused by indents of the code in your document since lstlisting takes it’s contents verbatim it also takes the line indention of the source code. – Tobi Sep 07 '11 at 23:12
  • @gablin: 2. Could you please run the code of my answer with no changes and tell me if there are problems and maybe upload the .log file? – Tobi Sep 07 '11 at 23:13
  • @Tobi: With style=1 you used tikz ;-) – Marco Daniel Sep 07 '11 at 23:13
  • @gablin: 3. The warning about using \captionof outside a box can be ignored I guess … I think it’s not happy to be outside a (float) environment, but it works. – Tobi Sep 07 '11 at 23:22
  • @Tobi: Well, with the new example at least the indentation has disappeared. However, I still have the problem that \BeforeBeginEnvironment doesn't work (says it's undefined even though the package is installed), and that the listings are unnumbered in the caption. – gablin Sep 09 '11 at 12:16
  • @gablin: Which TeX versrion are you using? I’m workin with TeX Live 2011. Did you try to make an update? If an update doesn’t fix your problem send me your log file, please. – Tobi Sep 09 '11 at 13:18
  • It appears I got TexLive 2009-10 installed, which is also the latest "version" according to Ubuntu. Wonder how I'll get 2011 installed... – gablin Sep 12 '11 at 06:38
  • @gablin: Ahh, the \AtBeginEnvironment is new in etoolbox v2.1. I don’t know how to update on linux but I’m sure there is a way. As a workaround you could install the new etoolbox directly. – Tobi Sep 12 '11 at 09:07
  • @gablin: If an update isn’t possible you could write \begin{mdframed}\vspace{-0.7em}\begin{lstlisting} ... \end{lstlisting}\vspace{-0.5em}\end{mdframed} to get the right formatting. – Tobi Sep 12 '11 at 09:17
  • @Tobi: Yep, that's what I've done to get the example to compile. But I still don't get any listings number printed in the caption; instead I get "Listing: yadda yadda yada". – gablin Sep 12 '11 at 12:32
  • @gablin: I’m sorry but I still can’t reproduce your numbering problem. Maybe some more packages are outdated (?). I added my file list so you can check what the problem could be … However I recommend to update to TL2011. – Tobi Sep 12 '11 at 15:11
  • @Tobi: Yeah, I'll try updating to TeXLive 2011 and see if that solves the problem. – gablin Sep 12 '11 at 15:45
  • @Tobi: Ah, now I got everything working; I ran it on my Windows machine after having updated all installed packages in MiKTeX (I think I must have had an old version of caption which caused the listings number to be missing, and that's probably the same problem I have on my Ubuntu laptop). Thanks for all your help. I'm installing TeXLive 2011 on my laptop as we speak. ^^ – gablin Sep 14 '11 at 18:16
  • Cursed! When I tried moving the example code into my document, the labels got completely screwed up. Since this is only a minor detail, I'll give up on getting rounded corners in my listings for now... – gablin Sep 14 '11 at 19:56
  • @gablin: Could you pleas explain what you mean with “ labels got completely screwed up”? You could also adda picture to your original question … Maybe there are some conflicts with other packages … try this: Copy your code to a new document and delete everything step by step until the error is a way, then you know where the problem is caused and create a minimal example to append it to the question. – Tobi Sep 15 '11 at 09:39
4

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

Marco Daniel
  • 95,681
  • +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 07 '11 at 23:03
  • @Tobi: Now problem. But I will add this code to my documentation. – Marco Daniel Sep 07 '11 at 23:07
  • So mdframed is your package? And it uses TikZ internally? – Tobi Sep 07 '11 at 23:14
  • Checkt it on CTAN myself. Seems to be a useful package :-) Greetings from Osnabrück ;-) – Tobi Sep 07 '11 at 23:19
  • 1
    @Tobi: "Die Nordlichter" ;-) – Marco Daniel Sep 07 '11 at 23:24
  • 1
    Nicht so nördlich wie gablin … – Tobi Sep 07 '11 at 23:34
  • 1
    Hey, what are you guys saying about me? =P – gablin Sep 08 '11 at 09:39
  • 1
    @gablin: Tobi and I from the north of Germany. We call such people as "Nordlichter". But Tobi mentioned that you are the real "Nordlicht" ;-) – Marco Daniel Sep 08 '11 at 13:20
  • 1
    @gablin: Sorry for talking german O:-) Goole says: “The Northern Lights” “Not as far north as gablin” ;-) Marco explained it I think. (Maybe we should delete this comments since they aren’t useful for the community …) – Tobi Sep 12 '11 at 09:10
  • 1
    @Tobi: Au contraire! I find it very useful to know what "The Northern Lights" is in German. Ops, now I involved a bit of French there... =P – gablin Sep 12 '11 at 14:50
  • 1
    @gablin: :-) But in german “Nordlichter“ is not only the name of a [http://www.google.com/search?q=nordlicht&hl=de&safe=off&client=safari&rls=en&prmd=ivns&tbm=isch&tbo=u&source=univ&sa=X&ei=XR5uTuOPJoTFswaXw-zpCg&ved=0CEMQsAQ&biw=1273&bih=702](physical phenomenon) but also a metaphor for the people living in the northern part of the country … – Tobi Sep 12 '11 at 15:03