1

Possible Duplicate:
Caption parbox 6.0 pt too wide

I used caption and listings package in my .tex file as below:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{ %Settings for listings package.
%language=[ANSI]{C},
extendedchars=false,% 重要!解决中文跨页出错的问题
backgroundcolor=\color{lightgray},
basicstyle={\ttfamily},
breakatwhitespace=false,
breaklines=true,
captionpos=t,
commentstyle=\color{olive},
%directivestyle=\color{blue},
extendedchars=false,
frame=tb,%single,%shadowbox
framerule=0pt,
keywordstyle=\color{blue}\bfseries,
morekeywords={*,define,*,include...},
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
rulesepcolor=\color{red!20!green!20!blue!20},
showspaces=false,
showstringspaces=false,
showtabs=false,
%stepnumber=2,
stringstyle=\color{purple},
tabsize=4,
escapeinside=``,
xleftmargin={2em},
xrightmargin={2em},
%title=\lstname
}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\begin{document}
\begin{lstlisting}[language={[ANSI]C},label=example,caption=example]
int main(int argc, char ** argv)
{
    printf("Hello!\n");
    return 0;
}
\end{lstlisting}
\end{document}

But the caption has been eclipsed by the listings.

My problems are:

  1. How can I do to avoid the eclipse?

  2. How can I to set caption's left and right margin as in the listings?

  • 3
    Instead of providing a code snippet, show a complete, compilable minimal working example (MWE) that clearly illustrates your problem. – Werner Jul 28 '12 at 05:20
  • Hi Werner, i have inserted the complete code.Thanks. – netcoffee Jul 28 '12 at 05:58
  • Hello, I have fixed the eclipse problem by change the frame to none, and add abovecaptionskip=0em, belowcaptionskip=0em,framextopmargin=0pt,in listings's setting. But I dont know howto change the caption's length either, it's to long. – netcoffee Jul 28 '12 at 07:31
  • Have a look at the package caption. – Marco Daniel Jul 28 '12 at 07:38
  • Great! I changed setting as below \DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\dimexpr\textwidth-18\fboxsep\relax}{#1#2#3}}}, and it works well! Thanks to @egrep posted here:http://tex.stackexchange.com/questions/37907/caption-parbox-6-0-pt-too-wide – netcoffee Jul 28 '12 at 08:11

0 Answers0