自动换行 \multicolumn 表格单元格

自动换行 \multicolumn 表格单元格

estout我使用/esttab包将大量表格输出到 LaTeX统计软件。我经常想在表格底部添加长脚注,解释系数、来源等。estout 包使用\multicolumn{#}{l}{...}在表格底部插入脚注,但需要我弄清楚换行符。也就是说,它会打印几行\multicolumn{#}{l}{...} \\,但我必须告诉 estout 我想要在每一行上显示什么。这很麻烦。

我想找到一种方法来使表格\multicolumn长度换行。我知道我可以\multicolumn使用对齐p{<width>}说明符来换行,但是我能想到的所有宽度宏(\hsize、、等)都会导致表格长度换行(或我告诉它的任何固定分数),因此要么拉长我的表格,要么换行太短。 \textwidth\linewidth\multicolumn

基本上,我希望 LaTeX 自动设置表格宽度(否tabular*tabularx),但忽略\multicolumn设置表格宽度的目的,但让\multicolumn换行位于表格的整个宽度。

以下是我正在使用的表格类型的示例

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}
\centering
\caption{This is a table}
\begin{tabular}{l*{6}c}\toprule
& Something & Something & Something & Something & Something & Something \\\midrule
Amazing regression results & 100 & 100 & 100 & 100 & 100 & 100 \\\bottomrule
\multicolumn{7}{p{\textwidth}}{This is a footnote that's really really  really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really long}\\ %Ideally this should wrap without distorting the table
\end{tabular}
\end{table}
\end{document}

有什么想法吗?我觉得一定有一些包含表格宽度的 LaTeX 参数。

答案1

Werner 的答案非常接近我需要的,但我找到了一种更简化的方法来避免重复:设置\multicolumn\linewidth,最初重新设置为非常小。然后将整个表格环境(带有 \multicolumn)放入宏中。然后将宏放入\setbox\linewidth重置为框的宽度,然后将表格打印为宏。像这样:

\documentclass{article}
\usepackage{booktabs,calc}
\begin{document}
\begin{table}
\centering
\caption{This is a table}
\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}{l*{4}c}\toprule
& Something & Something & Something & Something  \\\midrule
Amazing regression results & 100 & 100 & 100 & 100  \\\bottomrule
\multicolumn{5}{p{\linewidth}}{This is a footnote that's really really  really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really long}\\ %Ideally this should wrap without distorting the table
\end{tabular}}
\setbox0=\hbox{\contents}
\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}
\contents
\end{table}
\end{document}

这与 Werner 的答案类似,首先将表设置在未打印的框中,然后使用框的大小来设置\multicolumn。但是这样,虽然从编译器的角度来看表在技术上是重复的,但您不必复制源代码,而且更容易组合在一起(以及从 Stata 输出,这是我的目标),因为您实际上只是用几个命令围绕表格环境。

更新 11/16

自从我写这篇文章以来,已经有几个关于 Stata 实现这种包装器方法的答案。我的方法(在我写问题和答案时设计的)解决了 BeingQuisitive 和 grossdpg 提到的问题,同时保留了 estout 选项的大部分功能。具体来说,我使用substitute()estout/esttab 上的选项,它将 LaTeX 输出中的给定字符串替换为另一个字符串。这样做而不是使用postfoot()prehead()允许您使用各种 estout 选项将内容添加到这些区域,而无需手动设置它们(包括例如title()longtable())。示例 Stata 代码:

#delimit ;
esttab * using output.tex, replace booktabs 
substitute(\begin{tabular} \setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}
           \end{tabular} \end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents
           {l}{\footnotesize {p{\linewidth}}{\footnotesize )
addnote{"This is a footnote that's really really  really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really long")
;

注意:我似乎记得默认substitute()将 _ 和 $ 分别替换为 _ 和 \$。如果您需要此功能,只需将其添加到替换选项中即可。

答案2

LaTeX 不会逐行排版tabular(或array)的内容。这样做的明显原因是列应该对齐(否则它们将不会被称为列...)。并且,正确排列它们的唯一方法是您(LaTeX 编译器)可以看到整个表格。所以,这是我的建议...

存储表的完整副本没有将行\multicolumn放在一个框中。然后,使用框的宽度来设置\multicolumn{<#>}{p<width>}{...}列的宽度。这是一个最小的工作示例:

\documentclass{article}
\usepackage[landscape]{geometry}% http://ctan.org/pkg/geometry
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{calc}% http://ctan.org/pkg/calc
\begin{document}

\begin{table}
  \centering
  \caption{This is a table}
  \setbox0=\hbox{% Store table in box0
    \begin{tabular}{l*{6}c}
      \toprule
      & Something & Something & Something & Something & Something & Something \\\midrule
      Amazing regression results & 100 & 100 & 100 & 100 & 100 & 100 \\\bottomrule
    \end{tabular}
  %}
  \begin{tabular}{l*{6}c}\toprule
    & Something & Something & Something & Something & Something & Something \\\midrule
    Amazing regression results & 100 & 100 & 100 & 100 & 100 & 100 \\\bottomrule
    \multicolumn{7}{p{\wd0-2\tabcolsep-.25em}}{This is a footnote that's really really 
    really really really really really really really really really really really 
    really really really really really really really really really really really 
    really really really really really really really really really really really 
    really really really really really really really really really really really 
    really really really really really really really really really really really 
    really really really really really really really really really really really 
    really really really really really really really really really really really 
    really really really really really really really really long} %Ideally this should wrap without distorting the table
  \end{tabular}
\end{table}

\end{document}

在上述文件中,geometry包裹加载只是为了改变页面方向,landscape因为您的 MWE 不适合纵向页面。calc包裹用于执行中缀长度计算 -p列规范的参数:\wd0-2\tabcolsep-.25em取框 0 的宽度\wd0,减去 2 次\tabcolsep,然后.25em。最后一个维度来自必须插入的额外空间booktabs的規則。

带有多列的 Booktabs 拉伸整个表格宽度

由于 LaTeX 排版表格的方式,我看不出有办法解决表格“重复”的问题。当然,tabular*tabularx环境是理想的(因为它在定义中指定了宽度),但这是不可能的。

答案3

关于 BeingQuisitive 建议的自动化解决方案还有几点说明:

为了使其正常工作,您不能使用 estout/esttab 命令中的 title() 或 longtable 选项,因为它们会强加干扰 rsandler 包装器的 LaTeX 表或 longtable 环境。

此外,正如我所学到的,旧版本(v3.19 之前)的脚注有 502 个字符的限制。原因实际上在 Ben Jann 的 estout.ado 程序中进行了注释:estout 代码使用 -:subinstr- 宏函数将 note() 选项中提供的文本替换为 postfoot() 中的 @note 占位符。出于某种原因,此宏函数有 502 个字符的限制。如果您的脚注太长,则不会替换 @note,而“@note”(文字)将是显示在脚注中的文本。

estout.ado 中出现此问题的确切位置是第 3339 行(至少在 v3.13 中):

capt local value: subinstr local value "@`atvar'" `"`macval(`atvar')'"', all
// note: returns error if length of <to> is more than 502 characters

Ben Jann(estout 的作者)也在 Statalist 上讨论了这个问题: http://www.stata.com/statalist/archive/2007-02/msg00581.html

答案4

要将 rsandler 的解决方案放入自动化设置中,需要使用以下 Stata 代码:

*Estimate and Store results
local PostFootTxt "\hline\hline \multicolumn{@span}{p{\linewidth}}{\footnotesize @note}\\ \end{tabular}}"
local TblMainTxt = "This is a footnote that's really really really " + ///  
  "really really really really really really really really really " + ///
  "really really really really really long"
esttab stored_results_names using filename.tex, note("`TblMainTxt'") postfoot(`PostFootTxt')

然后 LaTeX(或类似的 LyX)文件将是:

\documentclass{article}
\usepackage{booktabs,calc}
\begin{document}
\begin{table}
\centering
\caption{This is a table}
\setlength{\linewidth}{.1cm}\newcommand{\contents}{\input{filename.tex}}
\setbox0=\hbox{\contents}
\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}
\contents
\end{table}
\end{document}

编辑:如果您有多个表格,那么您可以\let\contents\undefined在该\contents行后添加一行。

相关内容