The tabular*
is clumped together on the left because it had a required total width of \textwidth
, but the columns have no ability to stretch out to fill the width. The basic answer is to put \extracolsep{\fill}
in the tabular declaration, but that makes its own different ugly result.
The vertical lines give trouble in two ways: (1) The booktabs definitions don't work with them, leaving gaps at the horizontal rules; (2) the \extracolsep
problem is that the spaces stretch only on one side of each vertical line.
Before launching into messy solutions, let me exhort others to post answers using tabularx
perhaps using decimal alignment; or using even better tabular layout packages that do it elegantly!
First try: natural width without vertical lines -- Would this be reasonable? Some \quad
spacing is inserted to make the wider column headings be centered over the columns
\centering
\begin{tabular}{lll}\toprule
Model & Accuracy1& Accuracy2 \\ \midrule
Model1&\quad 0.421&\quad 18.47 \\
Model2 &\quad 0.46&\quad 23.62\\
Model3 &\quad 0.49 &\quad 25.7 \\ \bottomrule
\end{tabular}
Second try: stretched width without vertical lines -- This repeats the manual spacing with \quad
to center the heads over their columns, but alternatives are possible, as done further down.
\begin{tabular*}{\textwidth}{@{\qquad\extracolsep{\fill}}lll@{\qquad}}\toprule
Model & Accuracy1& Accuracy2 \\ \midrule
Model1&\quad 0.421&\quad 18.47 \\
Model2 &\quad 0.46&\quad 23.62\\
Model3&\quad 0.49 &\quad 25.7 \\ \bottomrule
\end{tabular*}
Third try, ruled tabular at natural width -- One must forego the extra spacing around the horizontal lines done by booktabs, but the result of that is too ugly to tolerate (\extrarowheight
does not solve the problem). Let's define some "struts" to enforce extra spacing around \hline
\newcommand\highstrut{\leavevmode\raise\jot\copy\strutbox}
\newcommand\deepstrut{\leavevmode\lower\jot\copy\strutbox}
Then use them in a ruled tabular above and below any \hline
\centering
\begin{tabular}{|l|l|l|}
\hline \highstrut
Model & Accuracy1 & Accuracy2\deepstrut \\ \hline
\highstrut
Model1 &\quad 0.421&\quad 18.47 \\
Model2 &\quad 0.46&\quad 23.62\\
Model3 &\quad 0.49 &\quad 25.7 \deepstrut\\ \hline
\end{tabular}
Fourth try: stretched with vertical lines -- where the spacing hits the fan. Since the only flexible spacing is between columns, and the rules are put in the column, one can't have equal spacing around the vertical lines unless they are placed in separate columns, and for that you need to abandon LaTeX's |
and use special "rule" columns like r@{\vline}
. Note the double &
signs! Think of them as representing the |
. Also, as long as there are large spaces between the columns, the headings can be centered over the (narrower) columns by letting them overhang the columns (\hidewidth
plus ~
).
\begin{tabular*}{\textwidth}{@{}
r@{\vline\extracolsep{\fill}} l
r@{\vline}l r@{\vline}l r@{\vline\extracolsep{0pt}}}
\hline
\highstrut & Model && \hidewidth~ Accuracy1\hidewidth~ &&
\hidewidth~ Accuracy2\hidewidth~ &\deepstrut \\ \hline
\highstrut & Model1 && 0.421 && 18.47 &\\
& Model2 && 0.46 && 23.62&\\
& Model3 && 0.49 && 25.7 &\deepstrut\\
\hline
\end{tabular*}
tabular
environment (without a star at the end) and delete the{\textwidth}
part. There are examples here: tex.stackexchange.com/questions/341205/….