表格中的多列未居中

表格中的多列未居中

我在使用 時遇到一些問題\multicolumn。從下面的螢幕截圖中可以看出,「Classical」和「Squared」一詞在列的中心正確對齊。然而,「指數」一詞並沒有正確居中。

在此輸入影像描述

代碼是:

\begin{table}[]
\setlength{\tabcolsep}{12pt}
\centering
\begin{tabular}{l c c c c c c}
\hline\hline
\\\\[-4.3\medskipamount]
 & \multicolumn{2}{c}{Classic} & \multicolumn{2}{c}{Squared} & \multicolumn{2}{c}{Exponential} \\
\textit{a} & 70 & 30 & 70 & 30 & 70 & 30 \\ [0.5ex]
\hline
\\\\[-3.9\medskipamount]
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
[1ex] \hline
\end{tabular}
\end{table}

我更願意在解決方案中繼續使用表格包。先感謝您!

答案1

原因是這個詞有點太長了。當然,你可以增加 的值\tabcolsep。另一種解決方案使用 a \makebox[0pt],它允許單字對稱地稍微重疊到列間空間。

除此之外,我建議使用booktabs具有可變厚度的規則,並在規則周圍添加一些垂直填充。這將使您免於手動調整間距:

\documentclass{article}
\usepackage{booktabs} 

\begin{document}

\begin{table}[]
\setlength{\tabcolsep}{12pt}
\centering
\begin{tabular}{l c c c c c c}
\toprule\midrule
 & \multicolumn{2}{c}{Classic} & \multicolumn{2}{c}{Squared} & \multicolumn{2}{c}{\makebox[0pt]{Exponential}} \\
\textit{a} & 70 & 30 & 70 & 30 & 70 & 30 \\ [0.5ex]
\midrule
\\\\[-3.9\medskipamount]
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
\bottomrule
\end{tabular}
\end{table}

\end{document} 

在此輸入影像描述

答案2

我將使用包S中的列類型siunitx,確定S列寬度並新增\cmidrule以下多列單元格:

\documentclass[12pt,a4paper]{article}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
    \begin{table}
\centering
\begin{tabular}{l *{6}{S[table-format=2,
                         table-column-width=2em]}
                }
    \toprule
            & \multicolumn{2}{c}{Classic} 
                            & \multicolumn{2}{c}{Squared} 
                                            & \multicolumn{2}{c}{Exponential} \\
    \cmidrule{2-3}\cmidrule(lr){4-5}\cmidrule{6-7}
\textit{a}  & 70    & 30    & 70    & 30    & 70    & 30 \\
    \midrule
S100        & 91    & 31    & 71    & 01    & 11    & 41 \\
S100        & 91    & 31    & 71    & 01    & 21    & 41 \\
S100        & 91    & 31    & 71    & 01    & 21    & 41 \\ 
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

在此輸入影像描述

答案3

使用的問題\makebox[0pt]{}在於,正如伯納德所說,長標題將與列間距重疊,這可能看起來相當難看。為了避免這種情況,兩個數字列的寬度總和應至少等於長標題的寬度。這可以在新的列類型中精確計算和定義,C如下所示。

\documentclass[12pt,a4paper]{article}
\usepackage{booktabs, array}    
\begin{document}

\begin{table}
\setlength{\tabcolsep}{12pt}
\newlength\wexp
\settowidth{\wexp}{Exponential}
\newcolumntype{C}{>{\centering\arraybackslash}p{\dimexpr.5\wexp-\tabcolsep}}

\centering
\begin{tabular}{l c c c c C C}
\toprule
           & \multicolumn{2}{c}{Classic} & \multicolumn{2}{c}{Squared} & \multicolumn{2}{c}{Exponential} \\
\textit{a} & 70 & 30 & 70 & 30 & 70 & 30 \\ \midrule
S100       & 91 & 31 & 71 & 01 & 11 & 41 \\
S100       & 91 & 31 & 71 & 01 & 21 & 41 \\
S100       & 91 & 31 & 71 & 01 & 21 & 41 \\ \bottomrule
\end{tabular}
\end{table}

\end{document}

在此輸入影像描述

答案4

我會用:

  1. w{<align>}{<wd>}列固定所有列的寬度
  2. \setlength{\tabcolsep}{0.5em}1em在列之間留出空間)
  3. 修剪過的cmidrule
  4. 無側軸承 ( @{})
  5. 標題中的字體較小一級
  6. booktab- 規則和addlinespace[<wd>]

在此輸入影像描述

還有 MWE:

\documentclass{article}
\usepackage{booktabs, array, caption} 

\begin{document}

\begin{table}[ht]
\caption{A table with figures\label{tab:atable}}
\setlength{\tabcolsep}{0.5em}
\centering
\begin{tabular}{@{}w{l}{4em}*{6}{w{c}{2em}}@{}}
\toprule
 & \multicolumn{2}{c}{\small Classic} & \multicolumn{2}{c}{\small Squared} & \multicolumn{2}{c@{}}{\makebox[0pt]{\small Exponential}} \\\cmidrule(l{0.75em}r{0.75em}){2-3}\cmidrule(l{0.75em}r{0.75em}){4-5}\cmidrule(l{0.65em}){6-7}
\textit{a} & 70 & 30 & 70 & 30 & 70 & 30 \\ 
\midrule\addlinespace[1ex]
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
S100 & 91 & 31 & 71 & 01 & 21 & 41 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

相關內容