我想将列表的标题放在中间,但我做不到。你知道我该如何实现吗?代码:
\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}