# Source code listing with frame around code?

I'd like to make my code listing looking more sexy. I found a good example on SO, but one problem is remaining:

I would like to not only have a horizontal line at the bottom of the listing but also at the left and right side. Therefore I changed frame=b to frame = blr. This works fine, but the problem is now that there is a small gap between the vertical lines and the caption box. How can I correct this? I want a closed box with a caption around my listing.

-
{looking-sexiy} may be nice, but {framed} is even better. ;-) – lockstep Apr 5 '11 at 0:19
Yes, I thought it is time for a tag to indicate questions that want to make a latex document looking more sexy ;) – RoflcoptrException Apr 5 '11 at 0:21

\documentclass{report}

\usepackage{xcolor}
\usepackage{listings}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{%
\parbox{\textwidth}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}\vskip-4pt}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\lstset{frame=lrb,xleftmargin=\fboxsep,xrightmargin=-\fboxsep}

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

\end{document}


-
Thanks but this is exactly the problem I have. If you look at the screenshot I made with your solution (img171.imageshack.us/img171/4317/testht.png) there is a gap between the vertical lines and the caption header. I wan't to remove this gap if possible. – RoflcoptrException Apr 5 '11 at 7:11
@Herbert: it looks if I add the following \usepackage{calc} \newlength\tdima \newlength\tdimb \setlength\tdima{ \fboxsep+\fboxrule} \setlength\tdimb{-\fboxsep+\fboxrule}  and then the listings options frame = tlrb, xleftmargin = \tdima, xrightmargin = \tdimb, if it works – Danie Els Apr 5 '11 at 7:24
@Danie Els Thanks this works, but I has a line on the top that is not looking really nice. Can I remove it somehow? – RoflcoptrException Apr 5 '11 at 7:51
@Roflcoptr: Make it the same color as the caption?, rulecolor= \color{gray}, – Danie Els Apr 5 '11 at 9:11
@Danie Els but then all lines are this color? And where do I have to put this? – RoflcoptrException Apr 5 '11 at 9:16

Although listings already offers tools for framed listings, I think tcolorbox is far more flexible for this task. In particular, tcolorbox can produce framed boxes for code listings processed/created with listings, listingsutf8 and minted.

Next code shows an example with the OP's desired sexy listing:

\documentclass{report}

\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{inconsolata}

\newtcblisting[auto counter]{sexylisting}[2][]{sharp corners,
fonttitle=\bfseries, colframe=gray, listing only,
listing options={basicstyle=\ttfamily,language=java},
title=Listing \thetcbcounter: #2, #1}

\begin{document}
\begin{sexylisting}{Some Code}
public void here() {
goes().the().code()
}
\end{sexylisting}

\begin{sexylisting}[colback=white]{Same code again}
public void here() {
goes().the().code()
}
\end{sexylisting}
\end{document}


-