將一組表格列置於寬標題下方居中

將一組表格列置於寬標題下方居中

在下表中,「平均一致性與隨機」列在邏輯上只是一列,但它需要一堆內部對齊:小數點應該全部對齊,不等號也應該對齊。為了實現內部對齊,我將其分為五列,再加上兩列用於填充,因為「平均一致與隨機」比不等式寬得多。我對這部分很滿意。

實際上問題我遇到的是我無法弄清楚如何使填充列自動調整大小,以便總體上不平等的區塊出現在標題下方的中心。現在我有使用m列的手動固定寬度填充,我可以透過手動調整使其看起來足夠好,但這很乏味,如果我再次更改整體佈局,則必須進行調整。

我嘗試將m列規格更改為m{0pt plus1fill},但“plus1fill”未被識別為寬度規格的一部分,而是顯示為嵌入在表格中。我還嘗試使用c填充列,\hspace{\fill}在兩者的頂部單元格中;這導致了不平等左齊平在他們的標題下。

建議?

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
% approximate \thickmuskip outside math mode
\def\relskip{\hskip 0.27778em plus0.27778em\relax}
\begin{tabular}[b]{lrm{2.5em}@{}r@{.}l@{\relskip}c@{\relskip}r@{.}l@{}m{1.5em}}
\toprule
  \textbf{Type} &
  \textbf{\# clusters} &
  \multicolumn{7}{c}{\textbf{Mean agreement vs. random}} \\
\midrule
\textbf{ASes}       & 534 & & 99 & 28\% & $>$   & 99 & 20\% & \\
\textbf{Cities}     & 481 & & 99 & 62   & $>$   & 98 & 99   & \\
\textbf{Countries}  &  96 & & 93 & 88   & $\ll$ & 96 & 32   & \\
\textbf{Continents} &   6 & & 83 & 08   & $<$   & 84 & 05   & \\
\bottomrule
\end{tabular}

\end{document}

上面 MWE 的渲染,最右列的對齊方式不太正確

答案1

這將計算將列居中所需的空間\multicolumn。我應該承認這-\tabcolsep更多是一個捏造因素,因為我不明白為什麼需要它。

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}
\newlength{\myspace}
\begin{document}
% approximate \thickmuskip outside math mode
\bgroup
  \sbox0{\textbf{Mean agreement vs. random}}
  \sbox1{\begin{tabular}{S@{}c@{}S}
    99.28\% & $>$ & 99.20\%
  \end{tabular}}% one row will do
  \global\myspace=\dimexpr 0.5\wd0-0.5\wd1-\tabcolsep\relax
\egroup
\begin{tabular}[b]{lr p{\myspace} S @{}c@{} Sl}
\toprule
  \textbf{Type} &
  \textbf{\# clusters} &
  \multicolumn{5}{c}{\textbf{Mean agreement vs. random}} \\
\midrule
\textbf{ASes}       & 534 & & 99.28\% & $>$   & 99.20\% & \\
\textbf{Cities}     & 481 & & 99.62   & $>$   & 98.99   & \\
\textbf{Countries}  &  96 & & 93.88   & $\ll$ & 96.32   & \\
\textbf{Continents} &   6 & & 83.08   & $<$   & 84.05   & \\
\bottomrule
&&\multicolumn{5}{c}{\rule{0.5pt}{\ht\strutbox}}
\end{tabular}

\end{document}

示範

答案2

我可能會使用更少的對齊點:

在此輸入影像描述

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\newcommand\p{\phantom{\%}}
\begin{tabular}{lr@{\qquad}r@{}>{${}}l<{{}$}@{}l}
\toprule
  \textbf{Type} &
  \multicolumn{1}{c}{\textbf{\# clusters}} &
  \multicolumn{1}{r@{}}{\textbf{Mean agreement}}&
  \multicolumn{2}{l}{\textbf{vs. random}} \\
\midrule
\textbf{ASes}       & 534 & 99.28\% & >&  99.20\%  \\
\textbf{Cities}     & 481 & 99.62\p & >&  98.99    \\
\textbf{Countries}  &  96 & 93.88\p &\ll& 96.32    \\
\textbf{Continents} &   6 & 83.08\p &  <& 84.05    \\
\bottomrule
\end{tabular}

\end{document}

答案3

我會縮短標題。

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}[htp]
\centering

\begin{tabular}{
  @{}
  l
  S[table-format=3.0]
  S[table-format=2.2]
  >{$}c<{$}
  S[table-format=2.2]
  @{}
  }
\toprule
Type &
{\# clusters} &
\multicolumn{3}{c@{}}{%
  \begin{tabular}[t]{@{}c@{}}Mean agreement \\ vs.\ random (\%)\end{tabular}%
} \\
\midrule
ASes       & 534 & 99.28 & >   & 99.20 \\
Cities     & 481 & 99.62 & >   & 98.99 \\
Countries  &  96 & 93.88 & \ll & 96.32 \\
Continents &   6 & 83.08 & <   & 84.05 \\
\bottomrule
\end{tabular}

\caption{Some caption}

\end{table}

\end{document}

在此輸入影像描述

答案4

下面的程式碼結合了 John Kormylo 和 預先計算表格寬度的想法tabularray。此外,表格序言指定:第一行和第一列的字體(不需要\textbf在所有單元格中使用),帶有不等號的列的數學類型({}在開頭和結尾添加以使用圍繞二元關係的通常數學間距)並消除帶有數字和不等號的列之間的間隙。數字使用 進行格式化siunitx,儘管這裡沒有必要(使用l列就足夠了)。

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs,siunitx}
\newdimen\myspace

\begin{document}
\bgroup
  \sbox0{%
  \begin{booktabs}{
    colspec={lll},
    row{1}={font=\bfseries},
  }
  Continents & \# clusters & Mean agreement vs. random \\
  \end{booktabs}%
  }
  \global\myspace=\wd0
\egroup

\begin{booktabs}[
  baseline=b,
]{
  width={\myspace},
  colspec={lrXS[table-format=2.2{\%}]cS[table-format=2.2{\%}]X},
  row{1}={font=\bfseries},
  column{1}={font=\bfseries},
  column{5}={mode=math,preto={{}},appto={{}},colsep=0pt},
  column{4}={rightsep=0pt},
  column{6}={leftsep=0pt},
  cell{1}{3}={c=5}{c},
}
\toprule
Type & \# clusters & Mean agreement vs. random \\
\midrule
ASes       & 534 & & 99.28\% & >   & 99.20\% & \\
Cities     & 481 & & 99.62   & >   & 98.99   & \\
Countries  &  96 & & 93.88   & \ll & 96.32   & \\
Continents &   6 & & 83.08   & <   & 84.05   & \\
\bottomrule
\end{booktabs}

\end{document}

結果: 在此輸入影像描述

相關內容