【问题标题】:LaTex listing and caption - How to exactly align frameLaTex 列表和标题 - 如何精确对齐框架
【发布时间】:2015-05-19 17:10:05
【问题描述】:

我想为我的列表使用格式良好的标题,如建议 LaTeX source code listing like in professional books。此外,内容应加框。

\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 配置边框。

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

【问题讨论】:

    标签: alignment latex listings


    【解决方案1】:

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

    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} 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多