2

I am looking for a way to format the first column of my table to look like this (to span the whole page width, mdpi template):

enter image description here

In my below code, the title of first column extends to overlap into the second column.

\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}

Output:

enter image description here

2
  • 1
    Without minimal working example, I can only guess, e.g., try l instead of X for the first column.
    – cabohah
    Sep 19 at 16:25
  • @cabohah that works, many thanks.
    – Amina Umar
    Sep 19 at 16:30

2 Answers 2

4

Since automatic line breaking isn't needed, I suggest you switch from a tabularx to a tabular* setup. No need for the adjustwidth sledgehammer.

enter image description here

\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

As supplement to nice @Mico answer (+1), as exercise for use of the tabularray package:

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .