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
  • 3
    {looking-sexiy} may be nice, but {framed} is even better. ;-)
    – lockstep
    Apr 5, 2011 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 ;) Apr 5, 2011 at 0:21

2 Answers 2

15
\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

8
  • 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. Apr 5, 2011 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, 2011 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? Apr 5, 2011 at 7:51
  • @Roflcoptr: Make it the same color as the caption?, rulecolor= \color{gray},
    – Danie Els
    Apr 5, 2011 at 9:11
  • @Danie Els but then all lines are this color? And where do I have to put this? Apr 5, 2011 at 9:16
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

2
  • 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 9, 2017 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 9, 2017 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.