幅の広いヘッダーの下にテーブル列のグループを中央揃えにする

幅の広いヘッダーの下にテーブル列のグループを中央揃えにする

次の表では、「平均一致 vs. ランダム」列は論理的には 1 列ですが、内部の調整が必要です。小数点はすべて一列に並び、不等号も一列に並びます。内部の調整を行うために、5 列に分割し、さらに 2 列をパディングに使用しました。「平均一致 vs. ランダム」は不等号よりも幅が広いためです。この部分はそのままで問題ありません。

実際の問題私が抱えている問題は、不等式のブロックが全体的にヘッダーの下に中央揃えで表示されるように、パディング列のサイズを自動調整する方法がわからないことです。現在、m列を使用して手動で固定幅のパディングを行っており、手動で調整することで見栄えを良くすることができますが、これは面倒で、全体のレイアウトを再度変更する場合は調整する必要があります。

m列の仕様を に変更しようとしましたが、「plus1fill」は幅の仕様の一部として認識されず、代わりにテーブルに埋め込まれて表示されます。また、両方の上部のセル内でパディング列にm{0pt 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すべてのセルで使用する必要はありません)、不等号のある列の数式タイプ ( {}2 項関係の周囲に通常の数式スペースを使用するために、先頭と末尾に を追加)、および数値と不等号のある列間のギャップの削除を指定します。数値は を使用して書式設定されていますが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}

結果: ここに画像の説明を入力してください

関連情報