表格超出列宽并与下一列的文本相冲突。如何强制压缩其宽度?

表格超出列宽并与下一列的文本相冲突。如何强制压缩其宽度?

我正在用 LaTex 编写一个包含 2 列的文档,其中我希望表格的宽度与列的宽度相同。我不太懂 LaTex,所以我使用在线编辑器创建表格的代码,它看起来几乎符合我的要求。代码如下:

\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}

当我生成表格时,它看起来像这张图所示: 在此处输入图片描述

主要问题是它太宽了。它不在列宽内,而是与右侧列中的文本相冲突。我怎样才能强制它调整表格中的空间以使其与列宽保持一致?

谢谢

答案1

我会做以下改变:

  • 对于表格环境使用tabularx规定一列的宽度
  • 减半\tabcolsep(3pt)
  • 减小字体大小至\small
  • 用于multirow第二列的标题
  • 定义新命令以弥补不足\multicolumn
  • 第一列定义为数学环境
  • 使用booktabs包中的规则
  • 用于threeparttable表格注释

梅威瑟:

\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}

在此处输入图片描述

答案2

你可以使用调整框并调整表格大小以适合所需的宽度。

\documentclass[preview]{standalone}
\usepackage{adjustbox}
\begin{document}
    \begin{table}[]
        \caption{My caption}
        \label{my-label}
            \begin{adjustbox}{max width=\textwidth}
                \begin{tabular}{llllll}
                    .....
                \end{tabular}
            \end{adjustbox}
    \end{table}
\end{document}

相关内容