1

我想为我的列表使用格式良好的标题,如 专业书籍中的 LaTeX 源代码列表中所建议的那样。此外,内容应加框。

\documentclass{report}

\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\lstdefinestyle{framed}
{
     frame=lrb,         
     belowcaptionskip=-1pt,
     xleftmargin=8pt,
     framexleftmargin=8pt,
     framexrightmargin=5pt,
     framextopmargin=5pt,
     framexbottommargin=5pt,
     framesep=0pt,
     rulesep=0pt,
 }

\begin{document}

\begin{lstlisting}[style=framed,label=some-code,caption=Some Code]
public void here() {
    goes().the().code()
}
\end{lstlisting}

\end{document}

结果是: 结果

可以清楚地看到,框架向左偏移了大约 1px。放大使我得出结论,这是由于字幕本身的边框设置造成的。但是我找不到任何信息如何使用 \captionsetup 配置边框。

非常感谢任何解决此问题的想法。

4

1 回答 1

0

其他人给了我一个很好的提示,只需手动将标题框稍微向左移动:

DeclareCaptionFont{white}{\color{white}} 
\DeclareCaptionFormat{listing}{% 
  \hspace*{-0.4pt}\colorbox{gray}{\parbox{\dimexpr\textwidth-2\fboxsep+.8pt\relax}{#1#2#3}}} 
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white} 
于 2015-03-19T21:05:34.033 回答