[Tex/LaTex] Caption parbox 6.0 pt too wide

boxescaptionscolor

For my listings, I use this caption setup:

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, margin=0pt, font={bf,footnotesize}}

For each listing, pdflatex warns me:

Overfull \hbox (6.0pt too wide) in paragraph at lines 129--129

In the PDF, one can easily see that it is indeed too wide there:

And a close up:

How can I get this to match exactly the width of the remaining contents?

Best Answer

A \colorbox adds some space before and after the text inside it; this space is \fboxsep wide; so

\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1#2#3}}

will do. You might also load the calc package and then write the same in the perhaps more natural syntax

\parbox{\textwidth-2\fboxsep}{#1#2#3}}