# Background color for the caption of a listing

I'm considering this piece of code

\lstinputlisting[
language=C,
breaklines=true,
basicstyle=\scriptsize\ttfamily,
numbers=none,
caption=\colorbox{mylightgray}\parbox{\textwidth}{\texttt{\detokenize{#1}}},
%title=\small\texttt\lstname,
frame=none]{#1}


why this use of parbox gives me an error? How I can have a caption in a box with a specific background color that spans for the entire \textwidth?

-
While code snippets are useful to explain specific points, it is better to provide a compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Nov 10 '11 at 21:33
@PeterGrill with \lstinputlisting it's involved another external file, i can't provide a working code on this site simply because it will be always incomplete, at least this way is clear and fully readable. –  Micro Nov 10 '11 at 21:38
You should provide as much as you can. In this case you could have provided two files, or combined them into one with the filecontents package. –  Peter Grill Nov 10 '11 at 21:42

Here's another approach using the features provided by the caption package to define a new format for listing captions:

\documentclass{article}
\usepackage{caption}
\usepackage{xcolor}
\usepackage{listings}

\DeclareCaptionFormat{myformat}{%
\colorbox{red!30}{\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}{#1#2\ttfamily#3}}
}
\captionsetup[lstlisting]{format=myformat}

\begin{document}

\begin{lstlisting}[caption={A colored caption}]
test
\end{lstlisting}

\end{document}


-
Why caption is not the same size as listing? Try to color the background of both to see this. Yout tried to fix this with \textwidth-2? Is there a better solution? –  sivic Sep 27 '12 at 20:47
@sivic thst's because the colored frame was designed to have a total width equal to the text width; that means that the text width inside the frame will be \textwidth-2\fboxsep (\fboxsep is the separation between the border of the frame and its contents); if you want to increase the width for the text, the frame will protrude into the margins and you will need to use something like \DeclareCaptionFormat{myformat}{% \makebox[\linewidth]{\colorbox{red!30}{\parbox{\dimexpr\textwidth-2\fboxsep\rela‌​x}{#1#2\ttfamily#3}} }} –  Gonzalo Medina Sep 27 '12 at 23:54
Since v3.3 of the caption package the option box=colorbox is available to make things easier. See tex.stackexchange.com/a/88206/2574 –  Axel Sommerfeldt May 16 '13 at 7:23

You could tap into the title key-value and construct the listing caption the way you want to using the following approach:

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{lipsum}% http://ctan.org/pkg/lipsum

\begin{filecontents*}{foo_bar.c}
void main{}
\end{filecontents*}

\begin{document}

\newcommand{\mylisting}[2][]{%
\refstepcounter{lstlisting}%
\lstinputlisting[language=C,
breaklines=true,
basicstyle=\scriptsize\ttfamily,
numbers=none,
title={\colorbox{black!10}{\parbox{\dimexpr\linewidth-6pt\relax}{\centering \lstlistingname~\thelstlisting:\ \texttt{\detokenize{#2}}}}},
frame=none,#1]%
{#2}%
}%

\lipsum[1]

\noindent\mylisting{foo_bar.c}

\lipsum[2]

\noindent\mylisting{foo_bar.c}

\lipsum[3]

\noindent\mylisting{foo_bar.c}

\lipsum[4]

\end{document}
​


However, this will require you to manage the "List of Listings" (\lstlistoflistings) by yourself, since no caption is used. Not sure whether you require this though...

-
ok, this works, but why the "listing " part still has a different font? I want to set the font to \texttt for that part. –  Micro Nov 10 '11 at 22:10
Are you looking for title={\colorbox{black!10}{\parbox{\dimexpr\linewidth-6pt\relax}{\centering\tex‌​ttt{\lstlistingname~\thelstlisting:\ \detokenize{#2}}}}}? –  Werner Nov 10 '11 at 22:11
this way the font changes but the counter does not advance and it remain stuck to 0, the output on the \thelstlisting part is always equal to 0. –  Micro Nov 10 '11 at 22:18
@Micro: The \mylisting command starts off with \refstepcounter{lstlisting}, incrementing the lstlisting counter with the appropriate reference anchor. You must be missing this in your macro if there is no increment in the counter or it always prints a 0. You'll see this is not the case in the graphic output in my answer. –  Werner Nov 10 '11 at 22:41

The other solutions are clearly superior, but this seems to work also:

Note that the filecontents package here is used just to be able to include the complete example in one file. It writes out foo_bar.c in the same directory so that it is available for use in via the lstinputlisting command. You don't need to use this in you real code as you will have the files available.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{filecontents}
\begin{filecontents*}{foo_bar.c}
void main{}
\end{filecontents*}

\renewcommand{\lstlistingname}{\texttt{Listing}}%

\lstdefinestyle{MyStyle}{
language=C,
breaklines=true,
basicstyle=\scriptsize\ttfamily,
numbers=none,
frame=none
}

\makeatletter
\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip
\sbox\@tempboxa{\colorbox{yellow}{#1: #2}}%
\ifdim \wd\@tempboxa >\hsize
\colorbox{yellow}{#1: #2}\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}%
\makeatother

\newcommand{\mylisting}[2][]{%
\lstinputlisting[style=MyStyle,caption=\texttt{\detokenize{#2}},#1]{#2}%
}

\begin{document}
\mylisting[frame=rlbt,language=C]{foo_bar.c}
\end{document}

-
the "Listing 1:" part is out of the box and the box itself doen't spans on the entire width. –  Micro Nov 10 '11 at 21:43
@Micro: See my answer to correct for this. –  Werner Nov 10 '11 at 22:00
You are not required to load the filecontents package explicitly if you're using it in your current form (within the document preamble). –  Werner Nov 10 '11 at 22:03
@Werner: Thanks for the info on filecontents. I did not know that. –  Peter Grill Nov 10 '11 at 22:21
@PeterGrill: I stand somewhat corrected. Read the filecontents package documentation - the standard filecontents environment (used without the filecontents package) does not allow overwriting of files; something that might be very useful depending on the application. Especially with this Q&A where/when you are playing around with code to answer an OP's question. –  Werner Nov 11 '11 at 15:50

You can combine caption and title:

\newcommand{\filelisting}[1]{%
\lstinputlisting[
language=C,
breaklines=true,
basicstyle=\scriptsize\ttfamily,
numbers=none,
caption=\texttt{\detokenize{#1}},
title=\lsttitle{#1},
frame=none]{#1}}

\newcommand{\lsttitle}[1]{\colorbox{mylightgray}{%
\makebox[\dimexpr\textwidth-2\fboxsep\relax]{%
\lstlistingname\ \thelstlisting: \ttfamily\detokenize{#1}}}}

-