![这样的表格该怎么写呢?](https://linux22.com/image/435423/%E8%BF%99%E6%A0%B7%E7%9A%84%E8%A1%A8%E6%A0%BC%E8%AF%A5%E6%80%8E%E4%B9%88%E5%86%99%E5%91%A2%EF%BC%9F.png)
但我发现这些列并没有严格居中。
如何修改代码才能使其正确?
\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}