![如何将包含换行符的表格标题居中](https://linux22.com/image/275755/%E5%A6%82%E4%BD%95%E5%B0%86%E5%8C%85%E5%90%AB%E6%8D%A2%E8%A1%8C%E7%AC%A6%E7%9A%84%E8%A1%A8%E6%A0%BC%E6%A0%87%E9%A2%98%E5%B1%85%E4%B8%AD.png)
我的表格标题跨度超过一行,因此我添加了一个\newline
以在适当的位置拆分标题。现在,两行左对齐。如何将标题的两行居中?
\begin{table}[htb]
\begin{center}
\caption{my caption line one\newline my caption line two}
\begin{tabular}{|r|c|c|c|c|c|}
\end{tabular}
\end{center}
\end{table}
答案1
软件包caption
允许多种定制。可以通过选项实现标题文本居中justification=centering
。
但是,由于手动换行,结果不太令人满意。因此,我在varwidth
包环境中添加了自己的居中格式varwidth
。它将行缩短到所需的长度。
第三个表格添加labelsep=newline
为表格标签添加第一行。
\documentclass{article}
\usepackage{caption}
\usepackage{varwidth}
\DeclareCaptionFormat{myformat}{%
% #1: label (e.g. "Table 1")
% #2: separator (e.g. ": ")
% #3: caption text
\begin{varwidth}{\linewidth}%
\centering
#1#2#3%
\end{varwidth}%
}
% \captionsetup{format=myformat}% global activation
\begin{document}
\begin{table}[htb]
\centering
\captionsetup{justification=centering}
\caption{my caption line one\newline my caption line two}
\begin{tabular}{|r|c|c|c|c|c|}
\hline
\verb|\captionsetup{justification=centering}|\\
\hline
\end{tabular}
\end{table}
\captionsetup{format=myformat}
\begin{table}[htb]
\centering
\caption{my caption line one\newline my caption line two}
\begin{tabular}{|r|c|c|c|c|c|}
\hline
\verb|\captionsetup{format=myformat}|\\
\hline
\end{tabular}
\end{table}
\captionsetup{labelsep=newline}
\begin{table}[htb]
\centering
\caption{my caption line one\newline my caption line two}
\begin{tabular}{|r|c|c|c|c|c|}
\hline
\verb|\captionsetup{format=myformat, labelsep=newline}|\\
\hline
\end{tabular}
\end{table}
\end{document}
已使用以下版本进行测试(\listfiles
):
*File List*
article.cls 2007/10/19 v1.4h Standard LaTeX document class
size10.clo 2007/10/19 v1.4h Standard LaTeX file (size option)
caption.sty 2013/05/02 v3.3-89 Customizing captions (AR)
caption3.sty 2013/05/02 v1.6-88 caption3 kernel (AR)
keyval.sty 1999/03/16 v1.13 key=value parser (DPC)
varwidth.sty 2009/03/30 ver 0.92; Variable-width minipages
***********
答案2
所以我刚刚试了一下,它有效。如果你使用\caption{some text}{then the new line}
它,它会打破两条线并使它们居中对齐。
答案3
\documentclass{article}
\begin{document}
\newlength{\temp}
\settowidth{\temp}{my caption line xxx }
\begin{table}[htb]
\begin{center}
\caption{\parbox[t]{\temp}{my caption line one\\ my caption line two}}
\begin{tabular}{|r|c|c|c|c|c|}
\end{tabular}
\end{center}
\end{table}
\end{document}
答案4
在两行之间不使用 \newline,而是使用 \\ 来使第二行居中。
\documentclass[12pt,a4paper]{article}
\usepackage{caption}
\usepackage{threeparttable}
\usepackage{multirow}
\begin{document}
\begin{table}[htbp] \centering%
\begin{threeparttable}
\captionsetup{justification=centering}
\caption{Canadian Pulp and Paper Production and Export \\
\small{In Thousand MT (\% World Share)}}%
\begin{tabular}{lcccccc}
\hline
Year & Production & Export & Production & Export & Production & Export \\
\hline
2005 & 25417 & 10612 & 19613 & 15731 & 7769 & 7324 \\
& (14.6) & (24.9) & (5.5) & 13.9 & (20.3) & (41.0)\\
\hline
\end{tabular}%
\end{threeparttable}
\end{table}
\end{document}