我正在使用 tabularx 来让表格适合文本宽度
\begin{table}[ht!]
\centering
\caption{Nice Title}
\scriptsize
\begin{threeparttable}[b]
\begin{tabularx}{\textwidth}{@{}l cccc cccc@{}}
\hline\hline
\addlinespace[2ex]
& \multicolumn{4}{c}{\textbf{Panel A: }} & \multicolumn{4}{c}{\textbf{Panel B: }} \\
\addlinespace[1ex]
\cmidrule(r){2-5} \cmidrule(l){6-9}
\addlinespace[1ex]
& Linear & KNN & G-Boost & MLP
& Linear & KNN & G-Boost & MLP \\
\addlinespace[1ex]
\hline
\addlinespace[1ex]
avg MAE & 0.201 & 0.019 & 0.042 & 0.057 & 0.179 & 0.181 & 0.056 & 0.059 \\
\addlinespace[1ex]
std MAE & (0.005) & (0.001) & (0.001) & (0.003) & (0.004) & (0.005) & (0.001) & (0.003) \\
\addlinespace[1ex]
avg MSE & 0.287 & 0.043 & 0.064 & 0.089 & 0.259 & 0.269 & 0.084 & 0.090 \\
\addlinespace[1ex]
std MSE & (0.009) & (0.005) & (0.002) & (0.008) & (0.008) & (0.008) & (0.002) & (0.004) \\
\addlinespace[1ex]
\hline
\end{tabularx}
\begin{tablenotes}[para,flushleft]
\linespread{1}\scriptsize
\item\hspace*{-\fontdimen2\font}Notes: Some Footnote here
\end{tablenotes}
\end{threeparttable}
\end{table}
这让我
也许我做的是不对的,我应该用可以 正常工作的方法\begin{tabularx}{\textwidth}
来 替换\begin{tabular}
它,但不知何故,我希望找到一个解决方案,其中 Latex 只会调整列的宽度以匹配文本宽度。
希望它是清楚的,当然也欢迎对错误代码的其他反馈。
更新: 如果我使用表格 x 中的 X 列类型,它可以解决表格适合宽度的问题,但是看起来不太好看。
如果我只拉伸第一列,也不会出现这种情况。如何让它看起来好看?
答案1
这个怎么样?
\documentclass[11pt]{article}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{tabularx, threeparttable}
\usepackage{siunitx}
\begin{document}
\begin{table}[ht!]
\centering
\sisetup{table-format=1.3, table-number-alignment=center, table-column-width=1cm, input-symbols=()}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{threeparttable}[b]
\caption{Nice Title}
\scriptsize
\begin{tabularx}{\textwidth}{@{}X SSSS!{\qquad}SSSS@{}}
\hline\hline
\addlinespace[2ex]
& \multicolumn{4}{c}{\textbf{Panel A: }} & \multicolumn{4}{c}{\textbf{Panel B: }} \\
\addlinespace[1ex]
\cmidrule(l{0.5em}r{2.5em}){2-5} \cmidrule(l){6-9}
\addlinespace[1ex]
& {Linear} & {KNN} & {G-Boost} & {MLP}
& {Linear} & {KNN} & {G-Boost} & {MLP} \\
\addlinespace[1ex]
\hline
\addlinespace[1ex]
avg MAE & 0.201 & 0.019 & 0.042 & 0.057 & 0.179 & 0.181 & 0.056 & 0.059 \\
\addlinespace[1ex]
std MAE & (0.005) & (0.001) & (0.001) & (0.003) & (0.004) & (0.005) & (0.001) & (0.003) \\
\addlinespace[1ex]
avg MSE & 0.287 & 0.043 & 0.064 & 0.089 & 0.259 & 0.269 & 0.084 & 0.090 \\
\addlinespace[1ex]
std MSE & (0.009) & (0.005) & (0.002) & (0.008) & (0.008) & (0.008) & (0.002) & (0.004) \\
\addlinespace[1ex]
\hline
\end{tabularx}
\begin{tablenotes}[para,flushleft]
\linespread{1}\scriptsize
\item\hspace*{-\fontdimen2\font}Notes: Some Footnote here
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}
答案2
由于在这九列中都不需要换行,甚至是不希望有换行符,因此我建议您使用tabular*
环境而不是tabularx
环境。
\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage[para,flushleft]{threeparttable}
\begin{document}
\begin{table}[ht!]
\caption{Nice Title\strut}
\small
\setlength\tabcolsep{0pt}
\setlength\extrarowheight{0.25ex}
\begin{threeparttable}%[b] % b??
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{8}{c} }
\toprule
& \multicolumn{4}{c}{\textbf{Panel A}}
& \multicolumn{4}{c}{\textbf{Panel B}} \\
\cmidrule{2-5} \cmidrule{6-9}
& Linear & KNN & G-Boost & MLP & Linear & KNN & G-Boost & MLP \\
\midrule
avg MAE & 0.201 & 0.019 & 0.042 & 0.057 & 0.179 & 0.181 & 0.056 & 0.059 \\
std MAE & (0.005) & (0.001) & (0.001) & (0.003) & (0.004) & (0.005) & (0.001) & (0.003) \\
\addlinespace
avg MSE & 0.287 & 0.043 & 0.064 & 0.089 & 0.259 & 0.269 & 0.084 & 0.090 \\
std MSE & (0.009) & (0.005) & (0.002) & (0.008) & (0.008) & (0.008) & (0.002) & (0.004) \\
\bottomrule
\end{tabular*}
\smallskip\scriptsize
\begin{tablenotes}
\linespread{1}
\item\hspace*{-\fontdimen2\font}Notes: Some Footnote here
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}