我用来listings
在文档中列出代码,我希望代码显示在具有圆角和灰色背景颜色的框架中。但是背景溢出了框架,就好像框架是正方形一样。
这是一个简单的例子:
\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}
结果:
有什么办法可以修复这个问题吗?
编辑
我现在可以让示例运行了。问题是由于所需软件包的版本太旧造成的。
答案1
根据我评论中的链接,我创建了一个可以在其中使用-environment 的环境lstlistings
:
代码
\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}
结果
局限性
此解决方案仅适用于非分页列表。请参阅答案测试某个段落是否有分页符?了解这是如何实现的。
查看更新 3
更新
我将这三行代码添加到上面的示例中,lstlisting
直接修补环境。因此无需\begin{code} … \end{code}
再手动输入。
\usepackage{etoolbox}
\BeforeBeginEnvironment{lstlisting}{\begin{code}}
\AfterEndEnvironment{lstlisting}{\end{code}}
更新 2
我创建了一个新命令\lstcapt[<Short>]{<Text>}
来设置框架外的标题,并将其添加到上面的示例代码中。
更新 3
我按照 Marco 的建议改用 mdframed 而不是直接使用 TikZ 来解决分页问题。
更新 4
请注意,此代码需要etoolbox
v2.1 或更高版本……
更新 5:文件列表
\filelist
这是之前的结果\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
***********
答案2
mdframed
您还可以使用允许分页的包:
\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}