Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 19 down vote accepted

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}}
share|improve this answer
    
Awesome, now it looks really neat! –  queueoverflow Dec 11 '11 at 14:27
1  
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:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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