1

I have a table on a two column-style page. My table goes over the page margin. I have tried using tabular, however, the headings are too close.

Table going over page margin

\begin{table}[h!]
\centering
 \begin{tabularx}{\textwidth}{@{} l *{3}{C} } 
 \hline
{} & Average Accuracy & Average BER & Average Precision \\
 \hline\hline
Best accuracy of SVM   &    0.000353 &     0.0187451 &     0.021746 \\
Best accuracy of SVM 1   &    0.000439 &     0.001381 &    0.015361 \\
Best accuracy of KNN    &    0.000894 &     0.013037 &     0.000438 \\
Best accuracy of KNN 1  &    0.000817 &     0.024353 &    0.0195106 \\
Best accuracy of NN  &    0.009123 &     0.0254135 &    0.010304 \\
Best accuracy of NN 1   &     0.00911 &     0.036151 &    0.0147809 \\
\bottomrule
 \end{tabularx}\hspace*{-25pt}
 \caption{Results for the models}
 \label{acc}
\end{table}

How can this be fixed?

mmcmp
  • 13
  • 1
    Welcome to TeX.SX! Did you see this: https://tex.stackexchange.com/q/332902/47927 ? – Jasper Habicht Oct 21 '22 at 16:43
  • If you write "Average Accuracy" etc on two lines, it will fit. Then you should normalize number printing, and maybe multiply them by 1000 and round to three significative digits so that people can really read them . Scaling should be only a last resource, font size will be different from the main text. – Rmano Oct 21 '22 at 16:47
  • \begin{tabularx}{\textwidth} is forcing the table to be twice as wide as the column a tabularx always has to have an X column, but you should use tabular here not tabularx – David Carlisle Oct 21 '22 at 17:18
  • How or where is the C column type defined? Do also please tell us which document class you employ. – Mico Oct 21 '22 at 17:39
  • @JasperHabicht Thank you for welcoming me! The link was helpful. – mmcmp Oct 21 '22 at 19:05
  • @Rmano Good suggestions. Thanks! – mmcmp Oct 21 '22 at 19:06
  • @DavidCarlisle I switched to tabular and put the column headers in two lines. – mmcmp Oct 21 '22 at 19:09
  • @Mico the detention for C is \newcolumntype{C}{>{\Centering\arraybackslash}X}. – mmcmp Oct 21 '22 at 19:12

2 Answers2

1

You mention that your document has a two-column layout. I assume you would like the table to fit in just one column. If this assumption is correct, the following solution may be of interest to you. It employs a tabular* environment (with width set to \columnwidth) and organizes the header material so that it takes up much less space than before. It also rounds the numbers in the data columns to 6 decimal digits.

enter image description here

\documentclass[twocolumn]{article}
\usepackage{booktabs,amsmath,siunitx}
% handy shortcut macro:
\newcommand\mytab[1]{\smash[b]{\begin{tabular}[t]{@{}l@{}} #1 \end{tabular}}}

\begin{document}

\begin{table}[h!] \setlength\tabcolsep{0pt} % let LaTeX figure out optimal column separation \sisetup{table-format=1.6,group-digits=false, round-mode=places,round-precision=6} \begin{tabular}{\columnwidth}{@{\extracolsep{\fill}} l {3}{S} } \toprule \mytab{Best accu-\racy of} & \multicolumn{3}{c@{}}{Average} \ \cmidrule(l){2-4} & {Accuracy} & {BER} & {Precision} \ \midrule SVM & 0.000353 & 0.0187451 & 0.021746 \ SVM 1 & 0.000439 & 0.001381 & 0.015361 \ KNN & 0.000894 & 0.013037 & 0.000438 \ KNN 1 & 0.000817 & 0.024353 & 0.0195106 \ NN & 0.009123 & 0.0254135 & 0.010304 \ NN 1 & 0.00911 & 0.036151 & 0.0147809 \ \bottomrule \end{tabular*} \caption{Results for the models} \label{tab:acc} \end{table}

\end{document}

Mico
  • 506,678
-1

The tabular is too big. Use the star version of table:

\documentclass[twocolumn]{article}
\usepackage{array,booktabs,graphicx,caption}
\usepackage{blindtext,showframe}% only for demo

\begin{document} \Blindtext \begin{table}[htb!]\centering \caption{Results for the models}\label{acc} \begin{tabular}{@{} l 3c } \hline & Average Accuracy & Average BER & Average Precision \\hline\hline Best accuracy of SVM & 0.000353 & 0.0187451 & 0.021746 \ Best accuracy of SVM 1 & 0.000439 & 0.001381 & 0.015361 \ Best accuracy of KNN & 0.000894 & 0.013037 & 0.000438 \ Best accuracy of KNN 1 & 0.000817 & 0.024353 & 0.0195106 \ Best accuracy of NN & 0.009123 & 0.0254135 & 0.010304 \ Best accuracy of NN 1 & 0.00911 & 0.036151 & 0.0147809 \\bottomrule \end{tabular} \end{table*} \Blindtext
\end{document}

enter image description here

user187802
  • 16,850
  • 1
    Please, never apply resizebox to tables – David Carlisle Oct 21 '22 at 17:17
  • I believe the OP wants the table to fit in a column, rather than span both columns of the textblock. If one changes your approach from \resizebox{\textwidth} to \resizebox{\columnwidth}, the resulting table will be well-nigh illegible. – Mico Oct 21 '22 at 18:17