带有标题的表格对于独立包文档来说太宽

带有标题的表格对于独立包文档来说太宽

我正在尝试使用该包输出带有标题的宽表standalone

当我在不带标题的情况下执行以下操作时,我得到了很好的输出:

\documentclass{standalone}

\usepackage{caption}
\usepackage{booktabs}

\begin{document}
%\minipage{0.9\textwidth}
\begin{tabular}{llllllllll}
\toprule
  & \multicolumn{3}{c}{Model 1}
  & \multicolumn{3}{c}{Model 2}
  & \multicolumn{3}{c}{Model 3}
\\
    \cmidrule(r){2-4}
    \cmidrule(r){5-7}
    \cmidrule(r){8-10}
%
  Variable
    & Coefficient & Std Error & $p$-value
    & Coefficient & Std Error & $p$-value
    & Coefficient & Std Error & $p$-value
\\
\midrule
  Brand1:$\log p_t$
    & -3.44 & 0.226 & 2e-16***
    & -3.39 & 0.227 & 2e-16***
    & -3.27 & 0.231 & 2e-16***
\\
  Brand2:$\log p_t$
    & -2.67 & 0.250 & 2e-16***
    & -2.62 & 0.248 & 2e-16***
    & -2.61 & 0.248 & 2e-16***
\\
  Brand3:$\log p_t$
    & -4.45 & 0.413 & 2e-16***
    & -4.50 & 0.425 & 2e-16***
    & -4.43 & 0.427 & 2e-16***
\\
  Brand4:$\log p_t$
    & -2.34 & 0.607 & 1e-4***
    & -2.32 & 0.601 & 1e-4***
    & -2.38 & 0.599 & 9e-5***
\\
  Brand5:$\log p_t$
    & -3.92 & 0.536 & 2e-12***
    & -3.90 & 0.532 & 2e-12***
    & -3.86 & 0.530 & 2e-12***
\\[5pt]
  Brand1:$\log p_{t-1}$
    & & &
    & 0.435 & 0.227 & 0.056.
    & 0.518 & 0.229 & 0.024*
\\
  Brand2:$\log p_{t-1}$
    & & &
    & 0.226 & 0.248 & 0.362
    & 0.256 & 0.248 & 0.302
\\
  Brand3:$\log p_{t-1}$
    & & &
    & 1.024 & 0.410 & 0.012*
    & 1.078 & 0.423 & 0.011*
\\
  Brand4:$\log p_{t-1}$
    & & &
    & 0.547 & 0.602 & 0.364
    & 0.543 & 0.598 & 0.364
\\
  Brand5:$\log p_{t-1}$
    & & &
    & 0.151 & 0.532 & 0.777
    & 0.183 & 0.531 & 0.731
\\[5pt]
  Brand1:$\log p_{t-2}$
    & & &
    & & &
    & 0.465 & 0.231 & 0.045*
\\
  Brand2:$\log p_{t-2}$
    & & &
    & & &
    & 0.241 & 0.248 & 0.332
\\
  Brand3:$\log p_{t-2}$
    & & &
    & & &
    & 0.067 & 0.413 & 0.870
\\
  Brand4:$\log p_{t-2}$
    & & &
    & & &
    & -0.508 & 0.599 & 0.397
\\
  Brand5:$\log p_{t-2}$
    & & &
    & & &
    & 0.047 & 0.530 & 0.979
\\[5pt]
  Brand1:MAPE
    & \multicolumn{3}{c}{0.146}
    & \multicolumn{3}{c}{0.131}
    & \multicolumn{3}{c}{0.114}
\\
  Brand2:MAPE
    & \multicolumn{3}{c}{0.111}
    & \multicolumn{3}{c}{0.111}
    & \multicolumn{3}{c}{0.109}
\\
  Brand3:MAPE
    & \multicolumn{3}{c}{0.108}
    & \multicolumn{3}{c}{0.103}
    & \multicolumn{3}{c}{0.099}
\\
  Brand4:MAPE
    & \multicolumn{3}{c}{0.192}
    & \multicolumn{3}{c}{0.192}
    & \multicolumn{3}{c}{0.194}
\\
  Brand5:MAPE
    & \multicolumn{3}{c}{0.110}
    & \multicolumn{3}{c}{0.103}
    & \multicolumn{3}{c}{0.099}
\\
\bottomrule
\end{tabular}
%\captionof{table}{Regression results for 5 brands of coffee in a single store.}
%\endminipage
\end{document}

输出无标题的代码

基于使用独立包创建表时出错,我尝试使用minipage环境和caption包来包含标题。代码和输出如下:

\documentclass{standalone}

\usepackage{caption}
\usepackage{booktabs}

\begin{document}
\minipage{0.9\textwidth}
\begin{tabular}{llllllllll}
\toprule
  & \multicolumn{3}{c}{Model 1}
  & \multicolumn{3}{c}{Model 2}
  & \multicolumn{3}{c}{Model 3}
\\
    \cmidrule(r){2-4}
    \cmidrule(r){5-7}
    \cmidrule(r){8-10}
%
  Variable
    & Coefficient & Std Error & $p$-value
    & Coefficient & Std Error & $p$-value
    & Coefficient & Std Error & $p$-value
\\
\midrule
  Brand1:$\log p_t$
    & -3.44 & 0.226 & 2e-16***
    & -3.39 & 0.227 & 2e-16***
    & -3.27 & 0.231 & 2e-16***
\\
  Brand2:$\log p_t$
    & -2.67 & 0.250 & 2e-16***
    & -2.62 & 0.248 & 2e-16***
    & -2.61 & 0.248 & 2e-16***
\\
  Brand3:$\log p_t$
    & -4.45 & 0.413 & 2e-16***
    & -4.50 & 0.425 & 2e-16***
    & -4.43 & 0.427 & 2e-16***
\\
  Brand4:$\log p_t$
    & -2.34 & 0.607 & 1e-4***
    & -2.32 & 0.601 & 1e-4***
    & -2.38 & 0.599 & 9e-5***
\\
  Brand5:$\log p_t$
    & -3.92 & 0.536 & 2e-12***
    & -3.90 & 0.532 & 2e-12***
    & -3.86 & 0.530 & 2e-12***
\\[5pt]
  Brand1:$\log p_{t-1}$
    & & &
    & 0.435 & 0.227 & 0.056.
    & 0.518 & 0.229 & 0.024*
\\
  Brand2:$\log p_{t-1}$
    & & &
    & 0.226 & 0.248 & 0.362
    & 0.256 & 0.248 & 0.302
\\
  Brand3:$\log p_{t-1}$
    & & &
    & 1.024 & 0.410 & 0.012*
    & 1.078 & 0.423 & 0.011*
\\
  Brand4:$\log p_{t-1}$
    & & &
    & 0.547 & 0.602 & 0.364
    & 0.543 & 0.598 & 0.364
\\
  Brand5:$\log p_{t-1}$
    & & &
    & 0.151 & 0.532 & 0.777
    & 0.183 & 0.531 & 0.731
\\[5pt]
  Brand1:$\log p_{t-2}$
    & & &
    & & &
    & 0.465 & 0.231 & 0.045*
\\
  Brand2:$\log p_{t-2}$
    & & &
    & & &
    & 0.241 & 0.248 & 0.332
\\
  Brand3:$\log p_{t-2}$
    & & &
    & & &
    & 0.067 & 0.413 & 0.870
\\
  Brand4:$\log p_{t-2}$
    & & &
    & & &
    & -0.508 & 0.599 & 0.397
\\
  Brand5:$\log p_{t-2}$
    & & &
    & & &
    & 0.047 & 0.530 & 0.979
\\[5pt]
  Brand1:MAPE
    & \multicolumn{3}{c}{0.146}
    & \multicolumn{3}{c}{0.131}
    & \multicolumn{3}{c}{0.114}
\\
  Brand2:MAPE
    & \multicolumn{3}{c}{0.111}
    & \multicolumn{3}{c}{0.111}
    & \multicolumn{3}{c}{0.109}
\\
  Brand3:MAPE
    & \multicolumn{3}{c}{0.108}
    & \multicolumn{3}{c}{0.103}
    & \multicolumn{3}{c}{0.099}
\\
  Brand4:MAPE
    & \multicolumn{3}{c}{0.192}
    & \multicolumn{3}{c}{0.192}
    & \multicolumn{3}{c}{0.194}
\\
  Brand5:MAPE
    & \multicolumn{3}{c}{0.110}
    & \multicolumn{3}{c}{0.103}
    & \multicolumn{3}{c}{0.099}
\\
\bottomrule
\end{tabular}
\captionof{table}{Regression results for 5 brands of coffee in a single store.}
\endminipage
\end{document}

带标题的代码输出

我该怎么做才能使输出不被切断?

答案1

在您的代码中,问题出现的原因在于 TeX 尝试对环境和标题材料使用相同的字体大小(即\normalsize) 。由于您使用的是相当窄的默认值,而且仅为 minipage 使用了该值的 90%,因此表格太宽,无法放入可用空间。tabular\textwidth

我建议您执行以下操作:(i) 使用较窄的列标题,(ii) 减小参数的值\tabcolsep(该参数控制列间空间的宽度),(iii) 不要在表格的开头和结尾使用任何空格,以及 (iv) 使用包将dcolumn六个数据列中的四个中的数字在小数点上对齐。通过这些更改,您将获得一个使用相同字体大小的表格,用于标题和环境内容tabular- 并适合文本块。

在此处输入图片描述

\documentclass{standalone}
\usepackage[margin=1in]{geometry}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{caption}
\newcolumntype{z}{D{.}{.}{2.3}}

\newcommand{\mc}[1]{\multicolumn{1}{c}{#1}}

\addtolength{\tabcolsep}{-0.5pt}

\begin{document}
\begin{minipage}{\textwidth}
\begin{tabular}{@{}lzzlzzlzzl@{}}
\toprule
  Variable
  & \multicolumn{3}{c}{Model 1}
  & \multicolumn{3}{c}{Model 2}
  & \multicolumn{3}{c@{}}{Model 3}
\\
    \cmidrule(lr){2-4}
    \cmidrule(lr){5-7}
    \cmidrule(l){8-10}
%
    & \mc{Coeff} & \mc{Std Err} & $p$-value
    & \mc{Coeff} & \mc{Std Err} & $p$-value
    & \mc{Coeff} & \mc{Std Err} & $p$-value\\
\midrule
  Brand1:$\log p_t$
    & -3.44 & 0.226 & 2e-16***
    & -3.39 & 0.227 & 2e-16***
    & -3.27 & 0.231 & 2e-16***
\\
  Brand2:$\log p_t$
    & -2.67 & 0.250 & 2e-16***
    & -2.62 & 0.248 & 2e-16***
    & -2.61 & 0.248 & 2e-16***
\\
  Brand3:$\log p_t$
    & -4.45 & 0.413 & 2e-16***
    & -4.50 & 0.425 & 2e-16***
    & -4.43 & 0.427 & 2e-16***
\\
  Brand4:$\log p_t$
    & -2.34 & 0.607 & 1e-4***
    & -2.32 & 0.601 & 1e-4***
    & -2.38 & 0.599 & 9e-5***
\\
  Brand5:$\log p_t$
    & -3.92 & 0.536 & 2e-12***
    & -3.90 & 0.532 & 2e-12***
    & -3.86 & 0.530 & 2e-12***
\\[5pt]
  Brand1:$\log p_{t-1}$
    & & &
    & 0.435 & 0.227 & 0.056.
    & 0.518 & 0.229 & 0.024*
\\
  Brand2:$\log p_{t-1}$
    & & &
    & 0.226 & 0.248 & 0.362
    & 0.256 & 0.248 & 0.302
\\
  Brand3:$\log p_{t-1}$
    & & &
    & 1.024 & 0.410 & 0.012*
    & 1.078 & 0.423 & 0.011*
\\
  Brand4:$\log p_{t-1}$
    & & &
    & 0.547 & 0.602 & 0.364
    & 0.543 & 0.598 & 0.364
\\
  Brand5:$\log p_{t-1}$
    & & &
    & 0.151 & 0.532 & 0.777
    & 0.183 & 0.531 & 0.731
\\[5pt]
  Brand1:$\log p_{t-2}$
    & & &
    & & &
    & 0.465 & 0.231 & 0.045*
\\
  Brand2:$\log p_{t-2}$
    & & &
    & & &
    & 0.241 & 0.248 & 0.332
\\
  Brand3:$\log p_{t-2}$
    & & &
    & & &
    & 0.067 & 0.413 & 0.870
\\
  Brand4:$\log p_{t-2}$
    & & &
    & & &
    & -0.508 & 0.599 & 0.397
\\
  Brand5:$\log p_{t-2}$
    & & &
    & & &
    & 0.047 & 0.530 & 0.979
\\[5pt]
  Brand1:MAPE
    & \multicolumn{3}{c}{0.146}
    & \multicolumn{3}{c}{0.131}
    & \multicolumn{3}{c}{0.114}
\\
  Brand2:MAPE
    & \multicolumn{3}{c}{0.111}
    & \multicolumn{3}{c}{0.111}
    & \multicolumn{3}{c}{0.109}
\\
  Brand3:MAPE
    & \multicolumn{3}{c}{0.108}
    & \multicolumn{3}{c}{0.103}
    & \multicolumn{3}{c}{0.099}
\\
  Brand4:MAPE
    & \multicolumn{3}{c}{0.192}
    & \multicolumn{3}{c}{0.192}
    & \multicolumn{3}{c}{0.194}
\\
  Brand5:MAPE
    & \multicolumn{3}{c}{0.110}
    & \multicolumn{3}{c}{0.103}
    & \multicolumn{3}{c}{0.099}
\\
\bottomrule
\end{tabular}
\captionof{table}{Regression results for 5 brands of coffee in a single store.}
\end{minipage}
\end{document}

答案2

我会把它分成两部分。

(1)我会将表格放入其自己的文件中。

(2)我将使用以下方式导入表格\includegraphics

调用你的表的文件mytable.tex

\documentclass{standalone}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{llllllllll}
    \toprule
      & \multicolumn{3}{c}{Model 1}
      & \multicolumn{3}{c}{Model 2}
      & \multicolumn{3}{c}{Model 3}
    \\
        \cmidrule(r){2-4}
        \cmidrule(r){5-7}
        \cmidrule(r){8-10}
    %
      Variable
        & Coefficient & Std Error & $p$-value
        & Coefficient & Std Error & $p$-value
        & Coefficient & Std Error & $p$-value
    \\
    \midrule
      Brand1:$\log p_t$
        & -3.44 & 0.226 & 2e-16***
        & -3.39 & 0.227 & 2e-16***
        & -3.27 & 0.231 & 2e-16***
    \\
      Brand2:$\log p_t$
        & -2.67 & 0.250 & 2e-16***
        & -2.62 & 0.248 & 2e-16***
        & -2.61 & 0.248 & 2e-16***
    \\
      Brand3:$\log p_t$
        & -4.45 & 0.413 & 2e-16***
        & -4.50 & 0.425 & 2e-16***
        & -4.43 & 0.427 & 2e-16***
    \\
      Brand4:$\log p_t$
        & -2.34 & 0.607 & 1e-4***
        & -2.32 & 0.601 & 1e-4***
        & -2.38 & 0.599 & 9e-5***
    \\
      Brand5:$\log p_t$
        & -3.92 & 0.536 & 2e-12***
        & -3.90 & 0.532 & 2e-12***
        & -3.86 & 0.530 & 2e-12***
    \\[5pt]
      Brand1:$\log p_{t-1}$
        & & &
        & 0.435 & 0.227 & 0.056.
        & 0.518 & 0.229 & 0.024*
    \\
      Brand2:$\log p_{t-1}$
        & & &
        & 0.226 & 0.248 & 0.362
        & 0.256 & 0.248 & 0.302
    \\
      Brand3:$\log p_{t-1}$
        & & &
        & 1.024 & 0.410 & 0.012*
        & 1.078 & 0.423 & 0.011*
    \\
      Brand4:$\log p_{t-1}$
        & & &
        & 0.547 & 0.602 & 0.364
        & 0.543 & 0.598 & 0.364
    \\
      Brand5:$\log p_{t-1}$
        & & &
        & 0.151 & 0.532 & 0.777
        & 0.183 & 0.531 & 0.731
    \\[5pt]
      Brand1:$\log p_{t-2}$
        & & &
        & & &
        & 0.465 & 0.231 & 0.045*
    \\
      Brand2:$\log p_{t-2}$
        & & &
        & & &
        & 0.241 & 0.248 & 0.332
    \\
      Brand3:$\log p_{t-2}$
        & & &
        & & &
        & 0.067 & 0.413 & 0.870
    \\
      Brand4:$\log p_{t-2}$
        & & &
        & & &
        & -0.508 & 0.599 & 0.397
    \\
      Brand5:$\log p_{t-2}$
        & & &
        & & &
        & 0.047 & 0.530 & 0.979
    \\[5pt]
      Brand1:MAPE
        & \multicolumn{3}{c}{0.146}
        & \multicolumn{3}{c}{0.131}
        & \multicolumn{3}{c}{0.114}
    \\
      Brand2:MAPE
        & \multicolumn{3}{c}{0.111}
        & \multicolumn{3}{c}{0.111}
        & \multicolumn{3}{c}{0.109}
    \\
      Brand3:MAPE
        & \multicolumn{3}{c}{0.108}
        & \multicolumn{3}{c}{0.103}
        & \multicolumn{3}{c}{0.099}
    \\
      Brand4:MAPE
        & \multicolumn{3}{c}{0.192}
        & \multicolumn{3}{c}{0.192}
        & \multicolumn{3}{c}{0.194}
    \\
      Brand5:MAPE
        & \multicolumn{3}{c}{0.110}
        & \multicolumn{3}{c}{0.103}
        & \multicolumn{3}{c}{0.099}
    \\
    \bottomrule
\end{tabular}
\end{document}

然后创建另一个文档来导入图像:

\documentclass{standalone}

\usepackage{caption}
\usepackage{booktabs}
\usepackage{graphicx}

\begin{document}
\begin{minipage}{0.9\textwidth}
  \includegraphics[width=\textwidth]{mytable}
\captionof{table}{Regression results for 5 brands of coffee in a single store.}
\end{minipage}
\end{document}

在此处输入图片描述

相关内容