1

I am trying to center the following LateX table:

\begin{table*}[ht]
    \caption{Comparison of optimization algorithms}
    \label{my-label}
    \begin{tabularx}{\textwidth}{|c|c|c|c|c|c|c|c|c|c|c|}
        \hhline{-*{10}{-}}
         Density & \multicolumn{3}{c|}{ 0.2 nodes/m } &  \multicolumn{3}{c|}{ 0.3 nodes/m } & \multicolumn{3}{c|}{ 0.2 nodes/m }\\
        \hhline{-*{10}{-}}
         Bandwidth & \multicolumn{3}{c|}{ 20 MHz } &  \multicolumn{3}{c|}{ 20 MHz } & \multicolumn{3}{c|}{ 120 MHz }\\
        \hhline{-*{10}{-}}
        Algorithm & DLNN & Bayesian & Grid & DLNN & Bayesian & Grid & DLNN & Bayesian & Grid \\
        \hhline{-*{10}{-}}
        Time & 40s & 85s & 575s & 45s & 90s & 573s & 45s & 85s & 658s \\
        \hhline{-*{10}{-}}
        Min CBR & 0.0618 & 0.0175 & 0.0168 & 0.02635 & 0.0272 & 0.0263 & 0.0022 & 0.0022  & 0.0021 \\
        \hhline{-*{10}{-}}
        $\gamma$ & 6.8 & 2.6 & 6.8 & 9 & 3.367 & 8.9 & 1.14 & 2.202 & 0.9 \\
        \hhline{-*{10}{-}}
        k & 10 & 8 & 8 & 10 & 8 & 10 & 10 & 10 & 10 \\
        \hhline{-*{10}{-}}
        $\lambda$ & 1 & 1  & 1  & 1  & 1  & 1  & 1  & 1  & 1 \\
        \hhline{-*{10}{-}}
        Nrp & 1 & 1  & 1  & 1  & 1  & 1  & 1  & 1  & 1 \\
        \hhline{-*{10}{-}}
        PRP & 0.9991 & 0.9993 & 0.9990 & 0.9990 & 0.9990 & 0.9990 & 0.9997 & 0.9997 & 0.9992 \\
        \hhline{-*{10}{-}}
        ED & 0.00040 & 0.00043 & 0.00040 & 0.00040 & 0.00046 & 0.00040 & 5.5E-05 & 5.6E-05 & 6.0E-05 \\
        \hhline{-*{9}{-}}
    \end{tabularx}
\end{table*}

I've tried \centering and \begin{center}. None of these worked, and I'm not sure why...

  • 1
    If you use \begin{tabularx}{\textwidth} the table spans the whole width. If you want to centre the table, reduce the width or switch to a normal tabular so that it can have its natural width. Is your page width enough for the table? – samcarter_is_at_topanswers.xyz Jan 24 '24 at 15:16
  • 2
    Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for the users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – samcarter_is_at_topanswers.xyz Jan 24 '24 at 15:17
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 24 '24 at 15:18
  • Depending on your page geometry, this might be a duplicate of https://tex.stackexchange.com/questions/332902/my-table-doesnt-fit-what-are-my-options , but without knowing how wide your pages are, hard to say... – samcarter_is_at_topanswers.xyz Jan 24 '24 at 15:22
  • 1
    a spanning table can not be h so you should remove [ht] which does nothing useful and will force the table to the end of the document if it is more that 70% of the page height – David Carlisle Jan 24 '24 at 15:27
  • @samcarter_is_at_topanswers.xyz I replaced it with normal tabular and it worked! Thank you so much! :) Next time I will be sure to format the code correctly – Cqllysto Jan 24 '24 at 18:42
  • Your code specifies that the table should have 11 columns; however, only 10 columns are actually needed. – Mico Jan 25 '24 at 01:13

1 Answers1

2

The OP's tabularx environment doesn't employ a single X-type column. Hence, achieving the stated target width (here: \textwidth) is doomed from the start.

A cursory inspection of the table further reveals that automatic line-wrapping is not needed anywhere. Hence, it would be more natural to employ a tabular* environment instead of a tabularx environment. Now, employing a tabular* environment properly requires the user to get rid of all vertical rules -- which is a good idea anyway. I'd also recommend fewer, but well-spaced, horizontal rules, with the help of the macros of the booktabs package.

I would further take care to provide some visually discernible structure to the header material. Merely providing a large number of horizontal rules does not qualify as furnishing a visually discernible structure.

enter image description here

\documentclass[twocolumn]{article} % or some other suitable document class
\usepackage{booktabs} % for \toprule, \midrule, \cmidrule, and \bottomrule macros

\begin{document}

\begin{table*} % span both columns of text \setlength\tabcolsep{0pt} % make LaTeX determine intercolumn spacing amount \caption{Comparison of optimization algorithms} \label{my-label}

\smallskip \begin{tabular}{\textwidth}{@{\extracolsep{\fill}} l {9}{c} } \toprule Density & \multicolumn{3}{c}{ 0.2 nodes/m } & \multicolumn{3}{c}{ 0.3 nodes/m } & \multicolumn{3}{c}{ 0.2 nodes/m } \

Bandwidth & \multicolumn{3}{c}{ 20 MHz } & \multicolumn{3}{c}{ 20 MHz } & \multicolumn{3}{c}{ 120 MHz } \ \cmidrule{2-4} \cmidrule{5-7} \cmidrule{8-10} Algorithm & DLNN & Bayesian & Grid & DLNN & Bayesian & Grid & DLNN & Bayesian & Grid \ \midrule Time, in seconds & 40 & 85 & 575 & 45 & 90 & 573 & 45 & 85 & 658 \

Min CBR & 0.0618 & 0.0175 & 0.0168 & 0.02635 & 0.0272 & 0.0263 & 0.0022 & 0.0022 & 0.0021 \

$\gamma$ & 6.8 & 2.6 & 6.8 & 9 & 3.367 & 8.9 & 1.14 & 2.202 & 0.9 \

$k$ &10 & 8 & 8 & 10 & 8 & 10 & 10 & 10 & 10 \

$\lambda$ & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \

Nrp & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \

PRP & 0.9991 & 0.9993 & 0.9990 & 0.9990 & 0.9990 & 0.9990 & 0.9997 & 0.9997 & 0.9992 \

ED & 0.00040 & 0.00043 & 0.00040 & 0.00040 & 0.00046 & 0.00040 & 5.5E-05 & 5.6E-05 & 6.0E-05 \ \bottomrule \end{tabular} \end{table}

\end{document}

Mico
  • 506,678
  • 1
    OP should also consider transposing the table, so that it becomes easier to compare numbers that should be similar. – Teepeemm Jan 25 '24 at 01:25