可能在xtable中创建乳胶多列? [英] possible to create latex multicolumns in xtable?

查看:55
本文介绍了可能在xtable中创建乳胶多列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有R Markdown和knitr的xtable来生成我用\ input {}调用的.tex文件.效果很好,但是我还没有弄清楚如何创建像这里.有人知道该怎么做吗?

I am using xtable with R Markdown and knitr to produce .tex files that I call with \input{}. Works great, but I have not figured out how to create multicolumns like the one shown here. Does anyone know how to to this?

到目前为止,我正在使用:

So far I am using:

tbl <- xtable(data, align="l r r r r r")
colnames(tbl) <- c("Variable", 
                  "Mean", "Std Dev", 
                  "Mean", "Std Dev", 
                  "Difference")
caption(tbl) <- c("Table Title")
print(tbl, 
    include.rownames=FALSE,
    caption.placement="top",
    booktabs=TRUE,
    type="latex", 
    file="output.tex")

我想对每个均值"和标准开发"(处理"和控制")使用不同的分组标题.

I'd like to have a different grouping header over each "Mean" and "Std Dev" ("Treatment" and "Control").

或者,有没有更好的方法可以使用R Markdown/knitr自动生成表?我不想手动编辑表格,因为报告需要自动生成.

Alternatively, is there a better method for using R Markdown/knitr to automatically generate tables? I don't want to manually edit the tables because the report needs to generate automatically.

更新: @agstudy:我是乳胶的新手,但是我想这是我希望通过xtable(或类似xtable的东西)自动生成的输出:

UPDATE: @agstudy: I'm new to latex, but I think this is the output I am looking to produce automatically with xtable (or something like xtable):

\begin{tabular}{lrrrrr}
  \toprule
      & \multicolumn{2}{c}{Treatment} & \multicolumn{2}{c}{Control} & \\
  \cmidrule(lr){2-3} \cmidrule(lr){4-5}
  Variable & Mean & Std Dev & Mean & Std Dev & Difference \\ 
  \midrule
  var1 & 1 & 2 & 3 & 4 & 5 \\ 
  \bottomrule
\end{tabular}

更新2: @乔纳森:我花了几读来了解你的建议.我接受了您的推荐,并且成功了.

UPDATE 2: @Jonathan: it took me a few reads to understand what you were suggesting. I took your recommendation, and it worked.

我现在在R markdown块中使用:

In the R markdown chunk I now use:

tbl <- xtable(data)
print(tbl,
    only.contents=TRUE,
    include.rownames=FALSE,
    type="latex",
    digits(tbl) <- c(0,1,1,1,1,1),
    file="output/tblout.tex")

然后在文本中,我使用:

Then in the text, I use:

\begin{tabular}{lddddd}
\toprule
    & \multicolumn{2}{c}{Treatment} & \multicolumn{2}{c}{Control} & \\
    \cmidrule(lr){2-3} \cmidrule(lr){4-5}
    Variable  &  \multicolumn{1}{r}{Mean}  &  \multicolumn{1}{r}{Std Dev}  &       \multicolumn{1}{r}{Mean}  &  \multicolumn{1}{r}{Std Dev}  &  \multicolumn{1}{r}{Difference}  \\
\midrule
\input{../output/tblout}
\bottomrule
\end{tabular}

我将看看是否有人对本机xtable(或其他软件包)解决方案有任何其他建议.否则,我会接受你的回答.谢谢!

I'll see if anyone has any other suggestions for a native xtable (or other package) solution. Otherwise, I will accept your answer. Thanks!

推荐答案

假设表的形式在每次运行中都是相同的(即,只有数字在变化),我的建议是使用only.contents参数print.xtable并手动编写多列标题.据我所知,xtable本身不具备处理多列细胞的能力.

Assuming the form of the table is the same across runs (i.e., only the numbers are changing), my suggestion would be to use the only.contents argument to print.xtable and code the multi-column headers in by hand. To the best of my knowledge xtable is not capable of doing multi-column cells itself.

这篇关于可能在xtable中创建乳胶多列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆