# listings caption gray box meaning of code

Some time ago I copied this code from Stackexchange to have a nice gray box at the top of my imported source code which I displayed with listings:

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\lstset{ aboveskip={0.0\baselineskip}, belowskip={0.0\baselineskip} }


The problem is that I have switched to minted to display code but I want to keep the gray title-boxes above the code. Can you help me to get rid of the need of the listings package? Here's an typical document:

\documentclass[a4paper,oneside]{article}
\usepackage{a4wide}
\usepackage[icelandic]{babel}
\usepackage{fontspec}
\usepackage{xunicode}

\usepackage{minted}
\usemintedstyle{perldoc}
\setlength\partopsep{-\topsep}

\usepackage{listings}
\usepackage{caption}
\usepackage{xcolor}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\lstset{ aboveskip={0.0\baselineskip}, belowskip={0.0\baselineskip} }

\begin{document}
\begin{lstlisting}[caption=\textbf{Title} -- This is the title I want for my code]
\end{lstlisting}
\begin{minted}{java}
public class demo {
public static void main(String[] args) {
System.out.println("My source code.");
}
}
\end{minted}
\end{document}


You could define your own (floating) lstlisting environment with \DeclareFloatingEnvironment offered by the newfloat package, e.g.:

\documentclass[a4paper,oneside]{article}
\usepackage{a4wide}
\usepackage[icelandic]{babel}
%\usepackage{fontspec}
%\usepackage{xunicode}

\usepackage{minted}
\usemintedstyle{perldoc}
\setlength\partopsep{-\topsep}

\usepackage{caption,newfloat}
\usepackage{xcolor}

\DeclareFloatingEnvironment[fileext=lol]{lstlisting}[Listing][List of Listings]
\captionsetup[lstlisting]{box=colorbox,boxcolor=gray,font={color=white},labelsep=endash,skip=5pt}

\begin{document}
\begin{lstlisting}
\caption{This is the title I want for my code}
\begin{minted}{java}
public class demo {
public static void main(String[] args) {
System.out.println("My source code.");
}
}
\end{minted}
\end{lstlisting}
\end{document}


Please note that I've changed the caption format with box=colorbox which is avail since caption.sty v3.3. If your caption package is older than that you need to either update or change it back to \DeclareCaptionFormat{listing}{...}.... See also: Center caption in listing

• That code gives me an error: ! Package caption Error: box undefined. See the caption package documentation for explanation. Changing caption package to caption2 compiles nicely and gives me this: [IMG]i.imgur.com/xwT7d1n.png[/IMG] What I'm looking for is something like this automatically above my code: {\colorbox{gray}{\parbox{\textwidth}{\color{white}{\textbf{Title} -- This is the title I want for my code}}}} – Bjarni Jens May 19 '13 at 0:09
• @pegasus As already written in my answer the option "box=" needs v3.3 (or newer) of the caption package, so updating your TeX distribution should help. (And caption2 is a dead smelly fish ;-)) – Axel Sommerfeldt May 19 '13 at 11:44