2

I want to write such a table, enter image description here

but I find that the columns are not strictly centered.

How to modify the code to make it correct?

\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}%
5
  • 1
    Welcome to TeX.SX! Please don't post code fragments. Instead, put your fragments into a complete compilable document that shows the problem. – NBur Jun 16 at 14:53
  • Why do you have extra tabulars in the middle of your table? – Teepeemm Jun 16 at 14:54
  • Do you really need $4$ decimal digits in the third column thi=rd row? – Bernard Jun 16 at 14:57
  • 1
    And 94.8000 is centered over 94.80. Do you want it centered like that, or do you want the decimal points to line up? – Teepeemm Jun 16 at 14:58
  • @NBur Sorrt for that, and the problem will be happen again next time. Indeed there is not need to do so, but that is the only way I knew to write such a table. Now I know the right way, as posted by Mico. Thank all of you for kindly help – 陈绍伍 Jun 16 at 16:13
6

I assume that by "strictly centered", you mean "centered on the decimal markers". If this interpretation is correct, the following solution may be of interest to you. Compared to the code you posted, it uses a single 5-column tabular environment and omits all interior tabular environments. It also (a) employs the S column type to perform alignment on the decimal markers and (b) provides two \cmidrule directives to provide the lines that are shown in the screenshot you posted.

enter image description here

\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}
2
  • Thanks a lot, your table is the exactly what I need, it help a lot – 陈绍伍 Jun 16 at 16:18
  • @陈绍伍 - You're most welcome! – Mico Jun 16 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.