我想将列表放在enumerate
环境中。lstlisting
标题的背景颜色取自这个帖子。然而,在放置lstlisting
下方之后item
,带有彩色框的标题超出了页面宽度,如以下示例所示:
\documentclass{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{color}
\definecolor{lstbgc}{gray}{0.9}
\definecolor{lstcapbg}{gray}{0}
\usepackage{listings}
\usepackage[scaled]{beramono}
\lstdefinestyle{myCpp}
{
language=C++,
tabsize = 4,
framesep = 3mm,
frame=none,
classoffset = 0,
columns = fixed,
basewidth = 0.5em,
basicstyle = \footnotesize\ttfamily,
keywordstyle = \bfseries\color[rgb]{0,0,1},
commentstyle = \itshape\color[rgb]{0.133,0.545,0.133},
stringstyle = \color[rgb]{0.627,0.126,0.941},
extendedchars = true,
escapeinside = {@}{@},
breaklines = true,
backgroundcolor=\color{lstbgc},
numbers = none
}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{%
\hspace*{-0.4pt}\colorbox{lstcapbg}{\parbox{\dimexpr\textwidth-2\fboxsep+.8pt\relax}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white,labelformat=empty}
\begin{document}
This is a long long long long long long long long long long long long long long long long sentence to see the actual margins.
\begin{lstlisting}[style=myCpp,title=example 0]
#include <iostream>
int main() {
return 0;
}
\end{lstlisting}
\begin{enumerate}
\item item 1
\begin{lstlisting}[style=myCpp,title=here is the long long long long long title for my short short short short short example 1]
#include <iostream>
int main() {
// a long long long long long long long long long long long long long long long long long long long long comment
return 0;
}
\end{lstlisting}
bla bla
\item item 2
\begin{lstlisting}[style=myCpp,title=here is the short title for example 2]
#include <iostream>
int main() {
// a long long long long long long long long long long long long long long long long long long long long comment
return 0;
}
\end{lstlisting}
\end{enumerate}
\end{document}
有没有什么办法可以解决这个问题?
答案1
正如评论中提到的,你用 来定义盒子宽度\textwidth
。你可以清楚地看到这太多了。此处的正确尺寸应为\linewidth
。
不过我建议您使用box=colorbox
包中的选项caption
:
% arara: pdflatex
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[scaled]{beramono}
\usepackage{xcolor}
\definecolor{lstbgc}{gray}{0.9}
\usepackage{listings}
\lstdefinestyle{myCpp}{%
language=C++,
tabsize = 4,
framesep = 3mm,
frame=none,
classoffset = 0,
columns = fixed,
basewidth = 0.5em,
basicstyle = \footnotesize\ttfamily,
keywordstyle = \bfseries\color[rgb]{0,0,1},
commentstyle = \itshape\color[rgb]{0.133,0.545,0.133},
stringstyle = \color[rgb]{0.627,0.126,0.941},
extendedchars = true,
escapeinside = {@}{@},
breaklines = true,
backgroundcolor=\color{lstbgc},
numbers = none
}
\usepackage{caption}[2015/09/20]
\captionsetup[lstlisting]{%
,box=colorbox
,boxcolor=black
,font={color=white}
,justification=RaggedRight
,singlelinecheck=off % in case you want to have short lines aligned left.
}
\begin{document}
This is a long long long long long long long long long long long long long long long long sentence to see the actual margins. % do you know \usepackage{showframe}?
\begin{lstlisting}[%
,style=myCpp
,title=example 0
]
#include <iostream>
int main() {
return 0;
}
\end{lstlisting}
\begin{enumerate}
\item item 1
\begin{lstlisting}[%
,style=myCpp
,title=here is the long long long long long title for my short short short short short example 1
]
#include <iostream>
int main() {
// a long long long long long long long long long long long long long long long long long long long long comment
return 0;
}
\end{lstlisting}
bla bla
\item item 2
\begin{lstlisting}[%
,style=myCpp
,title=here is the short title for example 2
]
#include <iostream>
int main() {
// a long long long long long long long long long long long long long long long long long long long long comment
return 0;
}
\end{lstlisting}
\end{enumerate}
\end{document}