我正在尝试将表格添加到我的 Latex 文档中,但表格的标题在左角缩小,并且没有延伸到整个表格的宽度。这是我的表格:
\usepackage{graphicx} % More fancy images
\usepackage[showframe=true]{geometry}
\usepackage{changepage}
\usepackage{ctable}% http://ctan.org/pkg/ctable
\usepackage{caption}% http://ctan.org/pkg/caption
\begin{document}
\begin{table}[!ht]
\footnotesize
\begin{adjustwidth}{-0.5cm}{}
\resizebox{0.7\textwidth}{!}{\begin{minipage}{\textwidth}
\caption{The constraints on the content of the universe based on best fit to the $\Lambda$CDM model. The first column represents the results from adding different cosmological probes.}
\begin{tabular}{ l c c c c p{5.cm} }
\hline\hline
Parameter & P+WP & P+WP+highL & P+WP+highL+L & P+WP+highL+L+BAO & Summary \\ \hline
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon \\
$\Omega_ch^2$& 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$& 0.14305 & ... & ... & ... & Total matter \\
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$[eV]&0.002&...&0.000&...&neutrino\\
$H_0$& 67.04&67.15&67.94&67.77&expansion rate \\\hline
\end{tabular}
\end{minipage}}
\end{adjustwidth}
\end{table}
\end{document}
我该如何解决这个问题?
答案1
\textwidth
您可以通过选择环境来设置表格的宽度tabularx
。唯一的技巧是,虽然应该将中间四列的主体指定为具有列类型S
(来自siunitx
包,以便数字在小数列上对齐),但应将列标题指定为具有(修改形式的)列类型X
(由包提供tabularx
);这允许将长标题行轻松分成两部分,同时让 LaTeX 为您计算出列宽。
booktabs
我还建议您 (a) 使用包 -- \toprule
、\midrule
和--的规则绘制宏\bottomrule
来获得间距适当的水平线以及 (b)...
用 替换省略号$\cdots$
。
使用此设置,可以指定\footnotesize
或者使用\resizebox
宏使表格适合文本块内是不必要的。换句话说,可以在不改变字体大小的情况下排版表格。从排版的角度来看,保持文档主要元素的字体大小一致通常被认为是可取的。
\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{booktabs} % \toprule, \midrule, and \bottomrule macros
\usepackage{siunitx} % for "S" column type
\usepackage{tabularx} % for "tabularx" environment
\usepackage[justification=justified, singlelinecheck=false]{caption}
\newcolumntype{C}{>{\arraybackslash\centering}X}
\newcommand\MC[1]{\multicolumn{1}{C}{#1}} % handy shortcut macro
\begin{document}
\begin{table}[!ht]
\sisetup{table-format=2.6, group-digits=false}
\caption{Constraints on the content of the universe based on best fit to the $\Lambda$CDM model.
The first column represents the results from adding different cosmological probes.}
\begin{tabularx}{\textwidth}{@{} l *{4}{S} l @{}}
\toprule
Parameter & \MC{P$+$WP} & \MC{P$+$WP$+$highL} & \MC{P$+$WP$+$highL $+$L} & \MC{P$+$WP$+$highL $+$L$+$BAO} & Summary \\
\midrule
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon\\
$\Omega_ch^2$& 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$& 0.14305 & {$\cdots$} & {$\cdots$} & {$\cdots$} & Total matter \\
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$[eV]&0.002& {$\cdots$} &0.000& {$\cdots$} &Neutrino\\
$H_0$& 67.04&67.15&67.94&67.77&Expansion rate \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
附录:我从您的评论中了解到,您可能有一个非常旧的 TeX 发行版,它不包含该siunitx
包,因此不知道该包的S
列类型。我发布了第二个解决方案,它以该dcolumn
包及其d
列类型为特色,以实现数字的十进制对齐。由于该dcolumn
包已经存在了 20 年左右,我希望您的 TeX 发行版做包括这个包。(如果你觉得下面的截图看起来确切地和上面的一样,确实正确。)
\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{booktabs} % \toprule, \midrule, and \bottomrule macros
\usepackage{dcolumn} % decimal alignment of numbers
\newcolumntype{d}[1]{D{.}{.}{#1}}
\usepackage{tabularx} % for "tabularx" environment
\usepackage[justification=justified, singlelinecheck=false]{caption}
\newcolumntype{C}{>{\arraybackslash\centering}X}
\newcommand\MC[1]{\multicolumn{1}{C}{#1}} % handy shortcut macro
\begin{document}
\begin{table}[!ht]
\caption{Constraints on the content of the universe based on best fit to the $\Lambda$CDM model.
The first column represents the results from adding different cosmological probes.}
\begin{tabularx}{\textwidth}{@{} l *{4}{d{2.6}} l @{}}
\toprule
Parameter
& \MC{P$+$WP}
& \MC{P$+$WP$+$highL}
& \MC{P$+$WP$+$highL $+$L}
& \MC{P$+$WP$+$highL $+$L$+$BAO}
& Summary \\
\midrule
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon\\
$\Omega_ch^2$& 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$& 0.14305 & \MC{$\cdots$} & \MC{$\cdots$} & \MC{$\cdots$} & Total matter \\
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$[eV]&0.002& \MC{$\cdots$} &0.000& \MC{$\cdots$} &Neutrino\\
$H_0$& 67.04&67.15&67.94&67.77&Expansion rate \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案2
一般来说,最好避免重新调整表格的比例;永远不要重新调整标题的比例。
您的主要问题是由于列标题太宽;我认为将表格延伸到整个行宽是没有用的,因此我将提出两种解决方案。
\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{caption}% http://ctan.org/pkg/caption
\usepackage{booktabs,tabularx,siunitx}
\newcommand{\splitcell}[1]{%
\begin{tabular}{@{}c@{}}\strut#1\strut\end{tabular}%
}
\newcommand{\var}[1]{\mathit{#1}}
\begin{document}
\begin{table}[!htp]
\centering\small
\caption{The constraints on the content of the universe based on best fit to the
$\Lambda$CDM model. The first column represents the results from adding different
cosmological probes.}
\begin{tabularx}{\textwidth}{
l
S[table-format=2.6]
S[table-format=2.6]
S[table-format=2.6]
S[table-format=2.6]
X
}
\toprule
Parameter
& {$P+\var{WP}$}
& {\splitcell{$P+\var{WP}\:+$\\$\var{highL}$}}
& {\splitcell{$P+\var{WP}\:+$\\$\var{highL}+L$}}
& {\splitcell{$P+\var{WP}\:+$\\$\var{highL}+L+\var{BAO}$}}
& Summary
\\
\midrule
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon \\
$\Omega_ch^2$ & 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$ & 0.14305 & {\dots} & {\dots} & {\dots} & Total matter \\
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$ (\si{eV}) & 0.002 & {\dots} & 0.000 & {\dots} & neutrino \\
$H_0$ & 67.04 & 67.15 & 67.94 & 67.77 & expansion rate $i$ \\
\bottomrule
\end{tabularx}
\end{table}
\begin{table}[!htp]
\centering
\caption{The constraints on the content of the universe based on best fit to the
$\Lambda$CDM model. The first column represents the results from adding different
cosmological probes.}
\begin{tabular}{
l
S[table-format=2.6]
S[table-format=2.6]
S[table-format=2.6]
S[table-format=2.6]
l
}
\toprule
Parameter
& {$P+\var{WP}$}
& {\splitcell{$P+\var{WP}\:+$\\$\var{highL}$}}
& {\splitcell{$P+\var{WP}\:+$\\$\var{highL}+L$}}
& {\splitcell{$P+\var{WP}\:+$\\$\var{highL}+L+\var{BAO}$}}
& Summary
\\
\midrule
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon \\
$\Omega_ch^2$ & 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$ & 0.14305 & {\dots} & {\dots} & {\dots} & Total matter \\
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$ (\si{eV}) & 0.002 & {\dots} & 0.000 & {\dots} & neutrino \\
$H_0$ & 67.04 & 67.15 & 67.94 & 67.77 & expansion rate $i$ \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
如您所见,您不需要重新调整表格的大小。
答案3
这是一个不需要\resizebox
也不adjustwidth
加载changepage
包,使用tabularx
环境,加上booktabs,
makecell (allowing for linebreaks in cells) and
siunitx` 来改善表格的外观的解决方案:
\documentclass{article}
\usepackage{mathtools}
\usepackage{booktabs}
\usepackage{graphicx} %
\usepackage[showframe=true]{geometry}
\usepackage{caption}%
\usepackage{array, tabularx}
\setlength\extrarowheight{2pt}
\usepackage{makecell}
\renewcommand\theadalign{rc}
\usepackage{siunitx}
\sisetup{table-format=1.6,table-number-alignment=center}
\begin{document}
\vspace*{1cm}
\begin{table}[!ht]
\footnotesize\captionsetup{format = hang, font=footnotesize, labelfont=sc}
\caption{The constraints on the content of the universe based on best fit to the $\Lambda$CDM model. The first column represents the results from adding different cosmological probes.}
\begin{tabularx}{\linewidth}{l *{4}{S}X}
\toprule\midrule
Parameter & {P+WP} & {\thead{P+WP+highL}} & {\thead{P+WP+highL\\+L\mbox{\hskip1.9em}}} & {\thead{P+WP+highL\\+L+BAO}} & Summary \\
\midrule\addlinespace
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon \\
$\Omega_ch^2$& 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$& 0.14305 & {...} & {...} & {...} & Total matter \\
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$[eV] & 0.002 & {...} & 0.000 & {...} &neutrino\\
$H_0$& {67.04} & {67.15} & {67.94} & {67.77} & expansion rate \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}