[Tex/LaTex] Table goes beyond column width and collides with text in the next column. How to force it to compress its width

columnstableswidth

I’m writing a document in LaTex with 2 columns where I want a table to ideally be of the same width as the column. I’m not that LaTex savvy so I used an online editor to create the code for my table, and it almost looks the way I want it. Here is the code:

\begin{table}[]
\caption{My caption}
\label{my-label}
\begin{tabular}{llllll}
\hline
Pre-processing & NV$^a$(LV$^b$) & \multicolumn{2}{l}{Calibration} & \multicolumn{2}{l}{Validation} \\ \cline{3-6} 
                 &          & RMSE            & R$^2$         & RMSE      & R$^2$               \\ \hline
$R(\lambda)$                      & X(X) & X    & X     & X       & X    \\
$D^1(R(\lambda))$                 & X(X) & X    & X     & X       & X    \\
$D^2(R(\lambda))$                 & X(X) & X    & X     & X       & X    \\
$-log(R(\lambda))$                & X(X) & X    & X     & X       & X    \\
$-log(D^1(R(\lambda))$            & X(X) & X    & X     & X       & X    \\
$-log(D^2(R(\lambda))$            & X(X) & X    & X     & X       & X    \\
$D^1(-log(R(\lambda)))$           & X(X) & X    & X     & X       & X    \\
$D^2(-log(R(\lambda)))$           & X(X) & X    & X     & X       & X    \\
$MSC(R(\lambda))$                 & X(X) & X    & X     & X       & X    \\
$MSC(D^1(R(\lambda)))$            & X(X) & X    & X     & X       & X    \\
$MSC(D^2(R(\lambda)))$            & X(X) & X    & X     & X       & X    \\
$MSC(-log(R(\lambda)))$           & X(X) & X    & X     & X       & X    \\
$MSC(D^1(-log(R(\lambda))))$      & X(X) & X    & X     & X       & X    \\
$MSC(-log(D^1(R(\lambda))))$      & X(X) & X    & X     & X       & X    \\
$MSC(D^2(-log(R(\lambda))))$      & X(X) & X    & X     & X       & X    \\
$MSC(-log(D^2(R(\lambda))))$      & X(X) & X    & X     & X       & X    \\
$SNV(R(\lambda))$                 & X(X) & X    & X     & X       & X    \\
$SNV(D^1(R(\lambda)))$            & X(X) & X    & X     & X       & X    \\
$SNV(D^2(R(\lambda)))$            & X(X) & X    & X     & X       & X    \\
$SNV(-log(R(\lambda)))$           & X(X) & X    & X     & X       & X    \\
$SNV(D^1(-log(R(\lambda))))$      & X(X) & X    & X     & X       & X    \\
$SNV(-log(D^1(R(\lambda))))$      & X(X) & X    & X     & X       & X    \\
$SNV(D^2(-log(R(\lambda))))$      & X(X) & X    & X     & X       & X    \\
$SNV(-log(D^2(R(\lambda))))$      & X(X) & X    & X     & X       & X    \\
Multiple simultaneous             & X(X) & X    & X     & X       & X    \\       \hline
\end{tabular}
\textit{$^a$Number of variables active in final variable     selection,$^b$optimal number of latent variables in PLS regression model.}
\end{table}

And when I generate the table it looks like in this image here:
enter image description here

The main problem is that it is too wide. It does not stay within the column width but collides with text in the column to the right. How can I force it to adjust the spaces in the table so it keeps to the column width?

Thanks

Best Answer

I would do the following changes:

  • use tabularx with prescribed width of one column for table environment
  • reduce \tabcolsep to half (3pt)
  • reduce font size to \small
  • use multirow for header of second column
  • define new command for shortness of \multicolumn
  • the first column define as math environment
  • use rules from booktabs packages
  • use threeparttable for table notes

MWE:

\documentclass[twocolumn]{article}
\usepackage{booktabs, multirow, tabularx, threeparttable}
\newcommand\mcc[1]{\multicolumn{2}{c}{#1}}
\usepackage{amsmath}

\usepackage{lipsum}% for dummy text

\begin{document}
\lipsum*[2]
    \begin{table}[htb]
\begin{threeparttable}[htb]
    \caption{My caption}
\label{my-label}
    \small%footnotesize
    \setlength\tabcolsep{3pt}
    \begin{tabularx}{\linewidth}{@{}>{$}l<{$} >{\centering}X cc cc@{}}
    \toprule
                            & \multirow{2}{=}{\centering 
                                               NV\tnote{a}   \\
                                              (LV\tnote{b})} 
                                   & \mcc{Calibration} & \mcc{Validation} \\ 
    \cmidrule(lr){3-4}\cmidrule(lr){5-6}
\text{Pre-processing}       &      & RMSE & R$^2$ & RMSE    & R$^2$\\ 
    \midrule
R(\lambda)                  & X(X) & X    & X     & X       & X    \\
D^1(R(\lambda))             & X(X) & X    & X     & X       & X    \\
D^2(R(\lambda))             & X(X) & X    & X     & X       & X    \\
-log(R(\lambda))            & X(X) & X    & X     & X       & X    \\
-log(D^1(R(\lambda))        & X(X) & X    & X     & X       & X    \\
-log(D^2(R(\lambda))        & X(X) & X    & X     & X       & X    \\
D^1(-log(R(\lambda)))       & X(X) & X    & X     & X       & X    \\
D^2(-log(R(\lambda)))       & X(X) & X    & X     & X       & X    \\
MSC(R(\lambda))             & X(X) & X    & X     & X       & X    \\
MSC(D^1(R(\lambda)))        & X(X) & X    & X     & X       & X    \\
MSC(D^2(R(\lambda)))        & X(X) & X    & X     & X       & X    \\
MSC(-log(R(\lambda)))       & X(X) & X    & X     & X       & X    \\
MSC(D^1(-log(R(\lambda))))  & X(X) & X    & X     & X       & X    \\
MSC(-log(D^1(R(\lambda))))  & X(X) & X    & X     & X       & X    \\
MSC(D^2(-log(R(\lambda))))  & X(X) & X    & X     & X       & X    \\
MSC(-log(D^2(R(\lambda))))  & X(X) & X    & X     & X       & X    \\
SNV(R(\lambda))             & X(X) & X    & X     & X       & X    \\
SNV(D^1(R(\lambda)))        & X(X) & X    & X     & X       & X    \\
SNV(D^2(R(\lambda)))        & X(X) & X    & X     & X       & X    \\
SNV(-log(R(\lambda)))       & X(X) & X    & X     & X       & X    \\
SNV(D^1(-log(R(\lambda))))  & X(X) & X    & X     & X       & X    \\
SNV(-log(D^1(R(\lambda))))  & X(X) & X    & X     & X       & X    \\
SNV(D^2(-log(R(\lambda))))  & X(X) & X    & X     & X       & X    \\
SNV(-log(D^2(R(\lambda))))  & X(X) & X    & X     & X       & X    \\
\text{Multiple simultaneous}& X(X) & X    & X     & X       & X    \\       
    \bottomrule
\end{tabularx}
    \begin{tablenotes}[para, flushleft]\itshape
\item[a]    Number of variables active in final variable selection,
\item[b]    optimal number of latent variables
        in PLS regression model.
    \end{tablenotes}
\end{threeparttable}
    \end{table}
\lipsum
\end{document}

enter image description here

Related Question