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.

I want to put the caption of my listing in the center but I can't do that. Do you have any idea how I could achieve this? The code:

\documentclass{report}
\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

% This concludes the preamble

\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
function <lhs_arguments>=<function_name><rhs_arguments>
  <statements>
endfunction
\end{lstlisting}

\end{document}
share|improve this question
2  
Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Dec 26 '12 at 4:15
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 at 18:52
add comment

migrated from stackoverflow.com Dec 26 '12 at 3:59

This question came from our site for professional and enthusiast programmers.

2 Answers

Use the following:

\DeclareCaptionFormat{listing}
  {\colorbox{gray}
     {\parbox{\dimexpr\textwidth-2\fboxsep}{\centering #1#2#3}}}

This not only centres the caption, but ensures that the width of the caption "box" fits exactly within the text block width.

enter image description here

\documentclass{report}
\usepackage{xcolor,listings,caption}% http://ctan.org/pkg/{xcolor,listings,caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}
  {\colorbox{gray}
     {\parbox{\dimexpr\textwidth-2\fboxsep}{\centering #1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

% This concludes the preamble

\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
function <lhs_arguments>=<function_name><rhs_arguments>
  <statements>
endfunction
\end{lstlisting}

\end{document}
share|improve this answer
1  
This solution will not only change "short" captions to be centered, but "long" captions (which does not fit into a single line), too. –  Axel Sommerfeldt Dec 26 '12 at 11:15
add comment

The caption format will be used internally after applying the justification & font setting, and therefore the \colorbox will be centered but not the text inside the \colorbox.

One can fix that by applying the justification setting inside the colorbox (again) using the internal command \caption@hj:

\documentclass{report}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{caption}
\makeatletter
\DeclareCaptionFormat{listing}{%
  \colorbox{gray}{%
    \parbox{\dimexpr \captionwidth-2\fboxsep}{\caption@hj #1#2#3}}}
\makeatother
\captionsetup[lstlisting]{format=listing,font={color=white}}

% This concludes the preamble

\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
function <lhs_arguments>=<function_name><rhs_arguments>
  <statements>
endfunction
\end{lstlisting}

\end{document}

(Please note that I replaced \textwidth with \captionwidth so the margin & width settings will be used here, too.)

But a more naturally way of fixing this is changing the internal code which actually draws the box around the caption, i.e. redefining \caption@parbox:

\documentclass{report}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{caption}[2007/12/23] % needs v3.1f or newer

\makeatletter
\DeclareCaptionOption{boxcolor}{%
  \renewcommand\caption@parbox[2]{%
    \colorbox{#1}{\parbox[b]{\dimexpr ##1-2\fboxsep}{##2}}}}
\makeatother
\captionsetup[lstlisting]{boxcolor=gray,font={color=white}}

% This concludes the preamble

\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
function <lhs_arguments>=<function_name><rhs_arguments>
  <statements>
endfunction
\end{lstlisting}

\end{document}

(Although both \caption@hj and \caption@parbox are not documented this will work with future versions of the caption package, too.)

When using version 3.3 of the caption package this can be realized without using internal commands at all:

\documentclass{report}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{caption}[2013/01/01] % needs v3.3 or newer
\captionsetup[lstlisting]{box=colorbox,boxcolor=gray,font={color=white}}

% This concludes the preamble

\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
function <lhs_arguments>=<function_name><rhs_arguments>
  <statements>
endfunction
\end{lstlisting}

\end{document}

Addendum 2013-01-09: Since the version 3.3 of the caption package is available now the last solution is preferable.

share|improve this answer
add comment

Your Answer

 
discard

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