I want to use a nicely formatted caption for my listings, as proposed in LaTeX source code listing like in professional books. Additionally the content should be framed.
\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}
Results in:
As you can clearly see, the frame is misaligned by about 1px to the left. Zooming in leads me to the conclusion that this is du to the border-settings of the caption itself. However I cannot find any information how to configure the border with \captionsetup.
Any ideas how to resolve this are very much appreciated.
Somebody else gave me a great hint, one just has to move the caption box slightly to the left manually:
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}