18

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.

2 Answers2

16
\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}

enter image description here

  • Thanks but this is exactly the problem I have. If you look at the screenshot I made with your solution (http://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 05 '11 at 07: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 05 '11 at 07: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 05 '11 at 07:51
  • @Roflcoptr: Make it the same color as the caption?, rulecolor= \color{gray}, – Danie Els Apr 05 '11 at 09:11
  • @Danie Els but then all lines are this color? And where do I have to put this? – RoflcoptrException Apr 05 '11 at 09:16
  • @Roflcoptr: I edited my answer ... –  Apr 05 '11 at 20:46
  • That's great! Is it possible to "break" the verbatim-style, such that I can use formulas inside it? (In my case, I need to use solely \Rightarrow) – aoeu Jul 12 '15 at 11:21
  • read the documentation. Search for mathescape –  Jul 12 '15 at 16:28
13

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}

enter image description here

Ignasi
  • 136,588
  • I feel like there's a lot of space before and after the code (Hence, it takes up quite some space on my paper). Is there any way to fix this? Or it's supposed to look better like that? – pineapple Oct 09 '17 at 11:00
  • 1
    @pineapple You can change tcolorbox inner margins with top, left, bottom, right and boxsep values. Try with top=0pt, bottom=0pt, boxsep=0pt and decide what you prefer. – Ignasi Oct 09 '17 at 11:40