tabularx:如何格式化该表的第一列?

tabularx:如何格式化该表的第一列?

我正在寻找一种方法来格式化表格的第一列,使其看起来像这样(跨越整个页面宽度,mdpi 模板):

在此处输入图片描述

在下面的代码中,第一列的标题延伸到与第二列重叠。

\begin{table}[H]
\caption{This is a wide table.\label{tab2}}
    \begin{adjustwidth}{-\extralength}{0cm}
        \newcolumntype{C}{>{\centering\arraybackslash}X}
        \begin{tabularx}{\fulllength}{XCCCCCCCCCCCC}
            \toprule
            Transportation &
      \multicolumn{4}{c}{Precision} &
      \multicolumn{4}{c}{Recall} &
      \multicolumn{4}{c}{F1-Score} \\
      \cmidrule(r{1ex}){2-5} \cmidrule(r{1ex}){6-9} \cmidrule{10-13}
      Mode & {RF} & {SVC} & {DT}  & {XGB}   & {RF} & {SVC} & {DT}  & {XGB}   & {RF} & {SVC} & {DT}  & {XGB}\\
      \midrule
      Foot  & 0.60 & 0.57 & 0.59   & 0.61 & 0.93 & 0.94 & 0.73   & 0.93 & 0.73 & 0.71 & 0.65   & 0.73 \\
      Bike  & 0.97 & 0.02 & 0.39   & 0.88 & 0.22 & 0.03 & 0.23   & 0.36 & 0.36 & 0.02 & 0.28   & 0.51 \\
      \bottomrule
      \end{tabularx}
      \end{adjustwidth}
\end{table}

输出:

在此处输入图片描述

答案1

由于不需要自动换行,我建议您从设置切换tabularxtabular*设置。无需大锤adjustwidth

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage{booktabs,amsmath}
\newcommand\mytab[1]{\smash[b]{%
   \begin{tabular}[t]{@{}l@{}} #1 \end{tabular}}}
  
\begin{document}
\begin{table}[ht]

%% \small % optional: 10% linear reduction in font size
\setlength{\tabcolsep}{0pt} % make LaTeX determine amount of intercolumn whitespace

\caption{The width of this table is \texttt{\string\textwidth}}\label{tab2}
        
\smallskip
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{12}{c} }
\toprule
\mytab{Transporta-\\tion mode} &
\multicolumn{4}{c}{Precision} &
\multicolumn{4}{c}{Recall} &
\multicolumn{4}{c@{}}{F1-Score} \\
\cmidrule{2-5} \cmidrule{6-9} \cmidrule{10-13}
& RF & SVC & DT & XGB & RF & SVC & DT & XGB & RF & SVC & DT & XGB \\
\midrule
Foot  & 0.60 & 0.57 & 0.59 & 0.61 & 0.93 & 0.94 & 0.73 & 0.93 & 0.73 & 0.71 & 0.65 & 0.73 \\
Bike  & 0.97 & 0.02 & 0.39 & 0.88 & 0.22 & 0.03 & 0.23 & 0.36 & 0.36 & 0.02 & 0.28 & 0.51 \\
\bottomrule
\end{tabular*}

\end{table}
\end{document}

答案2

作为对 @Mico 回答(+1)的补充,作为使用该tabularray包的练习:

\documentclass{article} 

\usepackage[skip=1ex]{caption}
\usepackage{ragged2e}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{table}[ht]
\caption{The width of this table is \texttt{\string\textwidth}}
\label{tab2}
\small
\begin{tblr}{colsep=2pt,
             colspec = {@{} X[2.5, cmd={\RaggedRight\hspace{0pt}}] *{12}{X[c]} @{}},
             cell{1}{2,6,10} = {c=4}{}
             }
    \toprule
\SetCell[r=2]{}     Transportation mode
    &   Precision
        &   &   &   &   Recall
                        &   &   &   &   F1-Score
                                        &   &   &   \\
    \cmidrule[lr]{2-5} \cmidrule[lr]{6-9} \cmidrule[l]{10-13}
    & RF & SVC & DT & XGB & RF & SVC & DT & XGB & RF & SVC & DT & XGB \\
    \midrule
Foot  & 0.60 & 0.57 & 0.59 & 0.61 & 0.93 & 0.94 & 0.73 & 0.93 & 0.73 & 0.71 & 0.65 & 0.73 \\
Bike  & 0.97 & 0.02 & 0.39 & 0.88 & 0.22 & 0.03 & 0.23 & 0.36 & 0.36 & 0.02 & 0.28 & 0.51 \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

相关内容