# Table goes beyond column width and collides with text in the next column. How do I force it to compress its width?

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:

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

• Put (LV$^b$) below NV$^a$. Abbreviate RMSE if it is wider than the cell contents below it. Remove the space to the left and right of the tabular by starting it with \begin{tabular}{@{}llllll@{}}. Reduce the inter-column space using \setlength\tabcolsep{...}. Choose a smaller font by adding \small before the table. – gernot Mar 24 '17 at 11:40
• @Petahanks, do your question is solved by comments or is one of received answers helpful to you? If the former is the case, than I will suggest to close your question as solved by comment. Otherwise, please please respond to answers' authors with comment or even better with voting or acceptation one of those answers. – Zarko Apr 24 '17 at 21:47

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}


You could use adjustbox and resize your table to fit the desired width.

\documentclass[preview]{standalone}