1

I am using tabularx to fit the table to textwidth

\begin{table}[ht!]
\centering
\caption{Nice Title}
\scriptsize
\begin{threeparttable}[b]
\begin{tabularx}{\textwidth}{@{}l cccc  cccc@{}}
\hline\hline
\addlinespace[2ex]
& \multicolumn{4}{c}{\textbf{Panel A: }} & \multicolumn{4}{c}{\textbf{Panel B: }}  \\ 
\addlinespace[1ex]

\cmidrule(r){2-5} \cmidrule(l){6-9} \addlinespace[1ex] & Linear & KNN & G-Boost & MLP
& Linear & KNN & G-Boost & MLP \ \addlinespace[1ex] \hline

\addlinespace[1ex] avg MAE & 0.201 & 0.019 & 0.042 & 0.057 & 0.179 & 0.181 & 0.056 & 0.059 \ \addlinespace[1ex] std MAE & (0.005) & (0.001) & (0.001) & (0.003) & (0.004) & (0.005) & (0.001) & (0.003) \ \addlinespace[1ex] avg MSE & 0.287 & 0.043 & 0.064 & 0.089 & 0.259 & 0.269 & 0.084 & 0.090 \ \addlinespace[1ex] std MSE & (0.009) & (0.005) & (0.002) & (0.008) & (0.008) & (0.008) & (0.002) & (0.004) \ \addlinespace[1ex] \hline

\end{tabularx} \begin{tablenotes}[para,flushleft] \linespread{1}\scriptsize \item\hspace*{-\fontdimen2\font}Notes: Some Footnote here \end{tablenotes} \end{threeparttable} \end{table}

Which gives me

enter image description here

Maybe what I am doing is not right, and I should just replace \begin{tabularx}{\textwidth} with \begin{tabular} which works fine, but somehow I was hoping to find a solution where Latex would just adjust the width of the columns to match textwidth.

Hope it is clear, and of course other feedback on bad code is welcome as well.

Update: If I use the X column type from tabular x it solves the problem of fitting the table to width, however the way it does not look nice.

enter image description here

Nor does it if I only stretch the first column. How to get this to look good?

enter image description here

Papayapap
  • 421
  • 2
    tabularx has to have at least one X column to stretch, but making the columns further apart just makes it harder for readers to read the table. – David Carlisle Apr 07 '21 at 15:05
  • @DavidCarlisle, indeed, I was just editing my question to include this. – Papayapap Apr 07 '21 at 15:06
  • 1
    If you only use X for the first column, only that column stretches. – David Carlisle Apr 07 '21 at 15:13
  • Yes, so it seems this auto-fitting with tabularx does not help? – Papayapap Apr 07 '21 at 15:15
  • well you could use X for all the columns (or all the columns except the first) or you could use tabular* rather than tabularx (that would be better) but I would not stretch at all here. – David Carlisle Apr 07 '21 at 15:17
  • For a better looking table, I suggest using the S column type from siunitx for all columns with numbers, in order to have an alignment on the decimal dot. – Bernard Apr 07 '21 at 15:20
  • @Bernard Does this work multicolumns inserted? It completely broke down my formatting when I tried it – Papayapap Apr 07 '21 at 15:31

2 Answers2

4

Since no linebreaks are needed or even desirable in any of the nine columns, I'd like to suggest that you employ a tabular* environment rather than a tabularx environment.

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs}
\usepackage[para,flushleft]{threeparttable}

\begin{document} \begin{table}[ht!]

\caption{Nice Title\strut} \small \setlength\tabcolsep{0pt} \setlength\extrarowheight{0.25ex} \begin{threeparttable}%[b] % b?? \begin{tabular}{\textwidth}{@{\extracolsep{\fill}} l {8}{c} } \toprule & \multicolumn{4}{c}{\textbf{Panel A}} & \multicolumn{4}{c}{\textbf{Panel B}} \

\cmidrule{2-5} \cmidrule{6-9}

& Linear & KNN & G-Boost & MLP & Linear & KNN & G-Boost & MLP \

\midrule

avg MAE & 0.201 & 0.019 & 0.042 & 0.057 & 0.179 & 0.181 & 0.056 & 0.059 \

std MAE & (0.005) & (0.001) & (0.001) & (0.003) & (0.004) & (0.005) & (0.001) & (0.003) \

\addlinespace

avg MSE & 0.287 & 0.043 & 0.064 & 0.089 & 0.259 & 0.269 & 0.084 & 0.090 \

std MSE & (0.009) & (0.005) & (0.002) & (0.008) & (0.008) & (0.008) & (0.002) & (0.004) \

\bottomrule

\end{tabular*}

\smallskip\scriptsize \begin{tablenotes} \linespread{1} \item\hspace*{-\fontdimen2\font}Notes: Some Footnote here \end{tablenotes} \end{threeparttable} \end{table} \end{document}

Mico
  • 506,678
  • 1
    Wish I could accept both answers, they both solve the problem and give me an indistinguishable output. – Papayapap Apr 07 '21 at 16:30
  • May I ask what the "\extracolsep{\fill}}" after opening the tabular is doing in the code? – Papayapap Apr 08 '21 at 08:35
  • 1
    @Papayapap - The directive @{\extracolsep{\fill}} tells LaTeX to insert something called \fill between the columns. In TeX and LaTeX, \fill generates freely stretchable whitespace. The overall effect is that the amount of whitespace in the 8 intercolumn areas will be the same -- and just enough to guarantee that the overall width of the tabular* environment will be \textwidth, i.e., the width of the text block. See this answer -- shameless self-citation alert -- for more information on this topic. – Mico Apr 08 '21 at 08:50
  • Great to know. Last question I promise: with this approach, it seems @{\extracolsep{\fill}} will fill equal space between columns and tabular* will adjust column size according to the size of contents, so not leading to equal column widths in cases when horizontal space becomes spare, no? – Papayapap Apr 08 '21 at 10:06
  • @Papayapap - Indeed, the @{\extracolsep{\fill}} approach serves to equalize the amounts of intercolumn whitespace; it does not affect the column widths. Now, whether or not the columns should have equal widths is an entirely separate question. Importantly, this question cannot be answered using abstract criteria alone. It matters what's in the table. In the specific case of your table, the column widths are quite similar; IMNSHO little to nothing would be gained from forcing the columns widths to be the same (and hence forcing the amounts of intercolumn whitespace to be non-constant). – Mico Apr 08 '21 at 12:26
1

How about this?

\documentclass[11pt]{article}
\usepackage{showframe} 
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{tabularx, threeparttable}
\usepackage{siunitx}

\begin{document}

\begin{table}[ht!] \centering \sisetup{table-format=1.3, table-number-alignment=center, table-column-width=1cm, input-symbols=()} \renewcommand{\tabularxcolumn}[1]{m{#1}} \begin{threeparttable}[b] \caption{Nice Title} \scriptsize \begin{tabularx}{\textwidth}{@{}X SSSS!{\qquad}SSSS@{}} \hline\hline \addlinespace[2ex] & \multicolumn{4}{c}{\textbf{Panel A: }} & \multicolumn{4}{c}{\textbf{Panel B: }} \ \addlinespace[1ex] \cmidrule(l{0.5em}r{2.5em}){2-5} \cmidrule(l){6-9} \addlinespace[1ex] & {Linear} & {KNN} & {G-Boost} & {MLP} & {Linear} & {KNN} & {G-Boost} & {MLP} \ \addlinespace[1ex] \hline \addlinespace[1ex] avg MAE & 0.201 & 0.019 & 0.042 & 0.057 & 0.179 & 0.181 & 0.056 & 0.059 \ \addlinespace[1ex] std MAE & (0.005) & (0.001) & (0.001) & (0.003) & (0.004) & (0.005) & (0.001) & (0.003) \ \addlinespace[1ex] avg MSE & 0.287 & 0.043 & 0.064 & 0.089 & 0.259 & 0.269 & 0.084 & 0.090 \ \addlinespace[1ex] std MSE & (0.009) & (0.005) & (0.002) & (0.008) & (0.008) & (0.008) & (0.002) & (0.004) \ \addlinespace[1ex] \hline \end{tabularx} \begin{tablenotes}[para,flushleft] \linespread{1}\scriptsize \item\hspace*{-\fontdimen2\font}Notes: Some Footnote here \end{tablenotes} \end{threeparttable} \end{table}

\end{document}

enter image description here

Bernard
  • 271,350