为什么我的表格列没有以正确的方式居中?

为什么我的表格列没有以正确的方式居中?

我有以下代码:

\usepackage{tabularx}
\begin{document}

\begin{table}[h]
    \caption{.\label{}}
    \begin{center}

            \begin{tabularx}{\textwidth}{X X X X X X X}
            \toprule
            \textbf{$T_{0}$} &\multicolumn{2}{c}{\textbf{Combination therapy}} & \multicolumn{2}{c}{\textbf{Chemotherapy}} & \multicolumn{2}{c}{\textbf{Immunotherapy}} \\
            \midrule
            &CIS&HIS&CIS&HIS&CIS&HIS\\
            \midrule
            $4.0\times 10^7$ & \multicolumn{2}{c}{\textbf{X}} & \multicolumn{2}{c}{\textbf{X}} & \multicolumn{2}{c}{\textbf{X}}\\
            $3.5\times 10^7$ & \quad\textbf{X} & \textbf{$\surd$} & \multicolumn{2}{c}{\textbf{X}} & \multicolumn{2}{c}{\textbf{X}}\\
            $2.5\times 10^7$ & \multicolumn{2}{c}{\textbf{$\surd$}} & \textbf{X} & \textbf{$\surd$} & \multicolumn{2}{c}{\textbf{X}}\\
            $3.2\times 10^6$ &  \multicolumn{2}{c}{\textbf{$\surd$}} & \multicolumn{2}{c}{\textbf{$\surd$}} &   \multicolumn{2}{c}{\textbf{X}}\\
            \bottomrule
        \end{tabularx}
    \end{center}
\end{table} 

\end{document}

但是,正如您在截图中看到的那样,列的宽度不正确:

enter image description here

我做错了什么?即使我这样做,结果也是一样的:

\begin{tabular}{c c c c c c c}
        \toprule
        \textbf{$T_{0}$} &\multicolumn{2}{c}{\textbf{Combination therapy}} & \multicolumn{2}{c}{\textbf{Chemotherapy}} & \multicolumn{2}{c}{\textbf{Immunotherapy}} \\
        \midrule
        &CIS&HIS&CIS&HIS&CIS&HIS\\
        \midrule
        $4.0\times 10^7$ & \multicolumn{2}{c}{\textbf{X}} & \multicolumn{2}{c}{\textbf{X}} & \multicolumn{2}{c}{\textbf{X}}\\
        $3.5\times 10^7$ & \quad\textbf{X} & \textbf{$\surd$} & \multicolumn{2}{c}{\textbf{X}} & \multicolumn{2}{c}{\textbf{X}}\\
        $2.5\times 10^7$ & \multicolumn{2}{c}{\textbf{$\surd$}} & \textbf{X} & \textbf{$\surd$} & \multicolumn{2}{c}{\textbf{X}}\\
        $3.2\times 10^6$ &  \multicolumn{2}{c}{\textbf{$\surd$}} & \multicolumn{2}{c}{\textbf{$\surd$}} &   \multicolumn{2}{c}{\textbf{X}}\\
        \bottomrule
 \end{tabularx}

enter image description here

答案1

一些建议和意见:

  • $\surd$您可能想要使用而不是\checkmark
  • X如果列的内容X应该居中而不是完全对齐,请设置列类型的居中版本。
  • 在“联合治疗”一词中确实允许换行。

enter image description here

\documentclass{article}  
\usepackage{tabularx,booktabs,amssymb}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[skip=0.333\baselineskip]{caption}
\newcommand\mc[1]{\multicolumn{2}{c}{#1}} % handy shortcut macro
\begin{document}

\begin{table}[h]
\caption{.\label{}}
\begin{tabularx}{\textwidth}{@{} c *{6}{C} @{}}
\toprule
\textbf{$T_{0}$} 
& \multicolumn{2}{>{\hsize=2\hsize\hspace{0pt}}C}{\textbf{Combination therapy}} % allow linebreak
& \multicolumn{2}{c}{\textbf{Chemotherapy}} 
& \multicolumn{2}{c@{}}{\textbf{Immunotherapy}} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-7} 
&CIS&HIS&CIS&HIS&CIS&HIS\\
\midrule
$4.0\times 10^7$ & \mc{\textbf{X}} & \mc{\textbf{X}} & \multicolumn{2}{c@{}}{\textbf{X}}\\
$3.5\times 10^7$ & \textbf{X} & \checkmark & \mc{\textbf{X}} & \multicolumn{2}{c@{}}{\textbf{X}}\\
$2.5\times 10^7$ & \mc{\checkmark} & \textbf{X} & \checkmark & \multicolumn{2}{c@{}}{\textbf{X}}\\
$3.2\times 10^6$ & \mc{\checkmark} & \mc{\checkmark} & \multicolumn{2}{c@{}}{\textbf{X}}\\
\bottomrule
\end{tabularx}
\end{table} 

\end{document}

答案2

这是由于第二列标题太宽。用 很容易将其分成两行makecell。我还用 和 中的真字形替换了我们的复选标记和叉号pifont,以siunitx简化以科学计数法输入数字:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pifont}
\usepackage{tabularx, booktabs, makecell, caption}
\renewcommand{\theadfont}{\boldmath\bfseries}
\usepackage{siunitx}

\begin{document}

\begin{table}[h]
  \caption{.\label{}}
  \centering
  \begin{tabularx}{\textwidth}{S[table-format =1.1e1]*{6}{ >{\centering\arraybackslash}X} }
    \Xhline{1pt}
    {\thead{$T_{0}$}} & \multicolumn{2}{c}{\thead{Combination \\ therapy}} & \multicolumn{2}{c}{\thead{Chemotherapy}} & \multicolumn{2}{c}{\thead{Immunotherapy}} \\
    \specialrule{\lightrulewidth}{0pt}{\belowrulesep}
                      & CIS & HIS & CIS & HIS & CIS & HIS \\
    \midrule
    4.0e7 & \multicolumn{2}{c}{\ding{56}} & \multicolumn{2}{c}{\ding{56}} & \multicolumn{2}{c}{\ding{56}} \\
    \addlinespace
    3.5e7 & \bfseries\ding{56} & \ding{52} & \multicolumn{2}{c}{\ding{56}} & \multicolumn{2}{c}{\ding{56}} \\
    \addlinespace
    2.5e7 & \multicolumn{2}{c}{\ding{52}} & \ding{56} & \ding{52} & \multicolumn{2}{c}{\ding{56}} \\
    \addlinespace
    3.2e6 & \multicolumn{2}{c}{\ding{52}} & \multicolumn{2}{c}{\ding{52}} & \multicolumn{2}{c}{\ding{56}} \\
    \bottomrule
  \end{tabularx}
\end{table}

\end{document} 

请注意,不应在或center内使用环境,因为这会增加不必要的垂直间距。请改用。 figuretable\centeringenter image description here

相关内容