但我发现这些列并没有严格居中。
如何修改代码才能使其正确?
\begin{table}[htbp]
\centering
\begin{tabular}{ccc}
\toprule
& Beijing & Shenzhen\\
& \begin{tabular}{cc}\midrule Price (\$)&Size\end{tabular}
& \begin{tabular}{cc}\midrule Price (\$)&Size\end{tabular}\\
\midrule
House1 & \begin{tabular}{cc}94.80& 94.8000\end{tabular} & \begin{tabular}{cc}94.80& 94.80\end{tabular} \\
House2 & \begin{tabular}{cc}94.80& 94.80\end{tabular} & \begin{tabular}{cc}94.80& 94.80\end{tabular} \\
House3 & \begin{tabular}{cc}94.80& 94.80\end{tabular}& \begin{tabular}{cc}94.80& 94.80\end{tabular} \\
\bottomrule
\end{tabular}%
\end{table}%
答案1
我假设您所说的“严格居中”是指“以小数点为中心”。如果这种解释是正确的,那么以下解决方案可能会让您感兴趣。与您发布的代码相比,它使用单个 5 列tabular
环境并省略所有内部tabular
环境。它还 (a) 使用S
列类型对小数点进行对齐,并且 (b) 提供两个\cmidrule
指令来提供您发布的屏幕截图中显示的行。
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx} % for 'S' column type
\begin{document}
\begin{table}[htbp]
\sisetup{table-format=2.2} % set the default layout
\centering
\begin{tabular}{@{} l S S[table-format=2.4] S S @{}}
\toprule
& \multicolumn{2}{c}{Beijing} & \multicolumn{2}{c@{}}{Shenzhen}\\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
& {Price (\$)} & {Size} & {Price (\$)} & {Size}\\
\midrule
House1 & 94.80 & 94.8000 & 94.80 & 94.80 \\
House2 & 94.80 & 94.80 & 94.80 & 94.80 \\
House3 & 94.80 & 94.80 & 94.80 & 94.80 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}