【问题标题】:Standalone latex table from amended xtable function来自修正的 xtable 函数的独立乳胶表
【发布时间】:2013-03-31 15:50:07
【问题描述】:

我正在尝试创建一个自动化的 R 函数,该函数创建一个可以由外部 Latex 程序运行(无需对 Latex 代码进行任何进一步修改)的乳胶文件。我知道 KnitR 和 Sweave,但我需要一些更简单的东西。基本上对于任何可以通过 xtable 打印出来的对象我都想添加

%A1 在顶部

\documentclass{article}
\pagestyle{empty}
\begin{document}

%A2 结尾

\end{document}

这意味着我可以运行我的分析并(当我需要时)创建一个可以由基于 Latex 的程序操作的外部文件。我对将所有表格放入一个文档不感兴趣,但我希望它们分开(因此需要 \begin{document} 和 \end{document}。我一直在尝试 add.to.rows(xtable 函数)但我无处可去。

例子:

data(tli)
tli.table <- xtable(tli[1:10,])
digits(tli.table)[c(2,6)] <- 0
print(tli.table,floating=FALSE)

如何将 A1 和 A2 相加,结果是:

\documentclass{article}
 \pagestyle{empty}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{rrlllr}
  \hline
 & grade & sex & disadvg & ethnicty & tlimth \\ 
  \hline
1 &   6 & M & YES & HISPANIC &  43 \\ 
  2 &   7 & M & NO & BLACK &  88 \\ 
  3 &   5 & F & YES & HISPANIC &  34 \\ 
  4 &   3 & M & YES & HISPANIC &  65 \\ 
  5 &   8 & M & YES & WHITE &  75 \\ 
  6 &   5 & M & NO & BLACK &  74 \\ 
  7 &   8 & F & YES & HISPANIC &  72 \\ 
  8 &   4 & M & YES & BLACK &  79 \\ 
  9 &   6 & M & NO & WHITE &  88 \\ 
  10 &   7 & M & YES & HISPANIC &  87 \\ 
   \hline
\end{tabular}
\end{table}
\end{document}

我一开始就卡住了:

bottom <- list();bottom$pos<- list()
bottom$pos[[1]] <- c(nrow(x))
bottom$command  <-c("\\end{document} \n")
print(xtable(tli[1:10,]),add.to.row=bottom)

我需要 \end{document} 在最后,但如果我改变了

bottom$pos

bottom$pos[[1]] &lt;- c(nrow(x)+3)

我得到一个错误。我也没有包括顶部(A1-见上文)。

理想情况下,我希望代码尽可能通用,以便可以将其应用于任何 xtable 输出(例如 anovas、侧边表等......)。

有什么想法吗?

非常感谢

【问题讨论】:

  • 你不能只使用\input 和带有序言材料的虚拟文件吗?

标签: r xtable


【解决方案1】:

catprint.xtable 函数都有一个“附加”参数:

wrapLatex <- function(dat,fil, ...) {
     cat(c("\\documentclass{article}\n",
           "\\pagestyle{empty}\n",
           "\\begin{document}\n"), file=fil)
   print(dat, file=fil, append=TRUE, ...)
      cat("\\end{document}\n", file=fil, append=TRUE) }
wrapLatex(tli.table, fil="~/test")

【讨论】:

  • 我不敢相信我忘记了这个帖子。我显然也忘了将它添加到我的 .Rprofile 文件中。在过去的 3 年里,我多次想要这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 2012-12-08
  • 1970-01-01
  • 2015-10-16
  • 2021-08-06
相关资源
最近更新 更多