6

I try to create a table in latex, however, the space between some columns is very unexpected.

Here is my working example:

\begin{table}[H]
    \centering
    \begin{tabular}{c c c c c c}
    \hline \hline
    & & The title of my table is long & &  \\ \hline \hline
     Criteria & M$_{1}$&  M$_{2}$ & M$_{3}$& M$_{4}$& M$_{5}$\\
    1 & 1400 & 1450& 300 &3340 & \\ 
    2 & & & & & \\
    3 & & & & & 
    \\ 
    \hline 
    \hline 
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label}
\end{table}

Here is the output

If the title of the table is short then, there is no problem.

Any help, please?

10

The title of your table is considered as an element in Column 3. This is a possible solution:

\documentclass{article}
\begin{document}
\begin{table}
    \centering
    \begin{tabular}{cccccc}
        \hline\hline
        \multicolumn{6}{c}{The title of my table is long}\\ 
        \hline\hline 
        Criteria & $M_1$ & $M_2$ & $M_3$ & $M_4$ & $M_5$ \\ 
        1        & 1400  & 1450  & 300   & 3340  & 0     \\
        2        &       &       &       &       &       \\
        3        &       &       &       &       &       \\
        \hline\hline
    \end{tabular}
    \caption{Caption}
\end{table}
\end{document}

enter image description here

12

As @DũngVũ has already pointed out in this answer, the immediate cause of the problem is that you've assigned the string "The title of my table is long" to the third column of the six-column table. That third column is thus made as wide as is needed to display the title string.

One way to fix this issue is to let the title string span all 6 columns of the tabular environment, via a \multicolumn{6}{c}{...} directive. This method "works", but only if the width of the title string is less than the width of the remaining tabular material. A second, and in my view more robust fix starts off by recognizing that the title string is, structurally speaking, part of the title of the table and that it should not be placed inside the tabular environment. Instead, it should be placed inside the argument of \caption, and the \caption statement should likely be placed above rather than below the tabular material.

While you're at it, you may also want to improve the "look" of the table by replacing the \hline statements with \toprule, \midrule, and \bottomrule -- macros provided by the booktabs package.

The following screenshot displays 3 tables: (1) the OP's original approach, (2) the fix achieved by using \multicolumn, and (3) the fix achieved by moving the title outside of the tabular environment (and using the line-drawing macros of the booktabs package).

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage[skip=0.333\baselineskip]{caption} % optional
\begin{document}

\begin{table}[ht!]  %% OP's original code
    \centering
    \begin{tabular}{c c c c c c}
    \hline \hline
    & & The title of my table is long & &  \\ 
    \hline \hline
    Criteria & M$_{1}$ & M$_{2}$ & M$_{3}$& M$_{4}$& M$_{5}$\\
    1 & 1400 & 1450& 300 &3340 & 4400  \\ 
    2 & & & & & \\
    3 & & & & & \\ 
    \hline \hline 
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label_1}
\end{table}

\begin{table}[h]  %% using \multicolumn to fix the spacing issue
    \centering
    \begin{tabular}{c c c c c c}
    \hline \hline
    \multicolumn{6}{c}{The title of my table is long}\\ 
    \hline \hline
    Criteria & M$_{1}$&  M$_{2}$ & M$_{3}$& M$_{4}$& M$_{5}$\\
    1 & 1400 & 1450& 300 &3340 & 4400  \\ 
    2 & & & & & \\
    3 & & & & & \\ 
    \hline \hline 
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label_2}
\end{table}

\begin{table}[h]  %% using \caption and the macros of the booktabs package
    \caption{The title of my table}
    \label{tab:my_label_3}
    \centering
    \begin{tabular}{@{} *{6}{c} @{}}
    \toprule
    Criteria & M\textsubscript{1}&  M\textsubscript{2} & M\textsubscript{3}& M\textsubscript{4}& M\textsubscript{5}\\
    \midrule
    1 & 1400 & 1450& 300 &3340 & 4400  \\ 
    2 & & & & & \\
    3 & & & & & \\ 
    \bottomrule
    \end{tabular}
\end{table}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.