列表中央的标题

列表中央的标题

我想将列表的标题放在中间,但我做不到。你知道我该如何实现吗?代码:

\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}

答案1

字幕格式将供内部使用应用对齐和字体设置,因此\colorbox将居中,但内部的文本不居中\colorbox

可以通过使用内部命令在颜色框内再次应用对齐设置来解决这个问题\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}

(请注意,我用 替换了,\textwidth因此\captionwidth这里也将使用边距和宽度设置。)

但解决这个问题更自然的方法是改变实际在标题周围绘制框的内部代码,即重新定义\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}

(尽管\caption@hj和都\caption@parbox没有记录,但这caption也将适用于该包的未来版本。)

当使用该软件包的 3.3 版本时,caption无需使用任何内部命令即可实现这一点:

\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}

附录 2013-01-09: 由于该软件包的 3.3 版本caption现已可用,因此最后一种解决方案是更好的选择。

答案2

使用以下内容:

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

这不仅使标题居中,而且确保标题“框”的宽度恰好适合文本块宽度。

在此处输入图片描述

\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}

相关内容