下面的代码有什么问题?

下面的代码有什么问题?
\begin{table}
  \caption{Description of the data sets}
  \newcolumntype{R}{>{\raggedright \arraybackslash} X}
  \newcolumntype{S}{>{\centering \arraybackslash} X}
  \newcolumntype{T}{>{\raggedleft \arraybackslash} X}
\begin{tabularx}{\linewidth} {>{\setlength\hsize{.30\hsize}}R >{\setlength\hsize{.17\hsize}}T >{\setlength\hsize{.18\hsize}}T >{\setlength\hsize{.17\hsize}}T >{\setlength\hsize{.18\hsize}}T}  % centered columns (4 columns)
  \toprule
  \multicolumn{3}{r}{Average Tree Nodes} & \multicolumn{5}{r}{Average Tree Depth} \\
  \cmidrule(l){2-3} \cmidrule(l){4-5}
  Data Set Name & R\_RF & OVA\_RF & R\_RF & OVA\_RF \tabularnewline
  \midrule
  Balance Scale & 103.5000 & 66.1717 & 3.0160 & 2.0111 \tabularnewline
  Car Evaluation & 129.5560 & 42.0550 & 4.8650 & 2.1050 \tabularnewline
  Dermatology & 17.6960 & 10.0304 & 3.5190 & 1.9441 \tabularnewline
  Ecoli & 43.4420 & 10.4885 & 7.9280 & 3.3712 \tabularnewline
  Glass Identification & 46.3860 & 13.9000 & 8.6480 & 4.0284 \tabularnewline
  Hayes-Roth & 14.8810 & 14.1778 & 2.6040 & 2.4626 \tabularnewline
  Iris & 10.0400 & 7.1859 & 3.8080 & 2.5778 \tabularnewline
  Lenses & 7.1740 & 5.7788 & 2.4010 & 2.0374 \tabularnewline
  Soybean (Small) & 6.4750 & 5.1700 & 1.8480 & 1.4020 \tabularnewline
  Statlog (Vehicle) & 150.5020 & 65.4100 & 14.3900 & 10.7180 \tabularnewline
  \midrule
  Average & 52.9652 & 24.0368 & 5.3027 & 3.2658 \tabularnewline
  \bottomrule
  \end{tabularx}
 \end{table}

答案1

您至少应该替换标题行

\multicolumn{3}{r}{Average Tree Nodes} & \multicolumn{5}{r}{Average Tree Depth} \\

& \multicolumn{2}{r}{Average Tree Nodes} & \multicolumn{2}{r}{Average Tree Depth} \\

您的tabularx环境包含五列,并且看起来标题应该分别跨越第 2/3 列和第 4/5 列。

我进一步建议重新定位最左上角的标题单元格。您可能不想右对齐四个数字列(大概是为了实现小数点标记上的数字对齐),而是希望 (i)对四个数字列使用包S的列类型,以及 (ii) 使用包的修改后的居中列类型(我已将其重命名以避免与 ...定义的类型发生冲突),以使表格跨越文本块的整个宽度。siunitxtabularxCSsiunitx

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx,booktabs}
\newcolumntype{C}{>{\centering \arraybackslash} X}
   %% reserve "S" for the column type defined by "siunitx"
\usepackage{siunitx} %% provides the "S" column type
\begin{document}
\begin{table}
\caption{Description of the data sets}
\smallskip % create a bit of separation between caption and tabular
\begin{tabularx}{\linewidth}{@{}l*{4}{S[table-format=2.4]}}
\toprule
Data Set Name 
& \multicolumn{2}{c}{Average Tree Nodes} 
& \multicolumn{2}{c}{Average Tree Depth} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
& % the use of "C" in next 4 column types ensures that table is expanded to width of \linewidth
\multicolumn{1}{C}{R\_RF} & 
\multicolumn{1}{C}{OVA\_RF} & 
\multicolumn{1}{C}{R\_RF} & 
\multicolumn{1}{C}{OVA\_RF} \\
\midrule
  Balance Scale & 103.5000 & 66.1717 & 3.0160 & 2.0111 \\
  Car Evaluation & 129.5560 & 42.0550 & 4.8650 & 2.1050 \\
  Dermatology & 17.6960 & 10.0304 & 3.5190 & 1.9441 \\
  Ecoli & 43.4420 & 10.4885 & 7.9280 & 3.3712 \\
  Glass Identification & 46.3860 & 13.9000 & 8.6480 & 4.0284 \\
  Hayes-Roth & 14.8810 & 14.1778 & 2.6040 & 2.4626 \\
  Iris & 10.0400 & 7.1859 & 3.8080 & 2.5778 \\
  Lenses & 7.1740 & 5.7788 & 2.4010 & 2.0374 \\
  Soybean (Small) & 6.4750 & 5.1700 & 1.8480 & 1.4020 \\
  Statlog (Vehicle) & 150.5020 & 65.4100 & 14.3900 & 10.7180 \\
\midrule
Average & 52.9652 & 24.0368 & 5.3027 & 3.2658 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

为了进行比较,这是您的原始表格的外观(\multicolumn根据我最初的建议修复了问题):

在此处输入图片描述

相关内容