简体   繁体   English

来自修正的xtable功能的独立乳胶台

[英]Standalone latex table from amended xtable function

I'm trying to create an automated R function which creates a latex file that can be run (without any further amendments to the Latex code) by an external Latex program. 我正在尝试创建一个自动R函数,它可以通过外部Latex程序创建一个可以运行的乳胶文件(不需要对Latex代码进行任何进一步的修改)。 I'm aware of KnitR and Sweave but I need something much simpler. 我知道KnitR和Sweave,但我需要更简单的东西。 Basically for any object that can be printed out through xtable I want to add 基本上对于任何可以通过xtable打印的对象我想添加

%A1 At the top %A1在顶部

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

%A2 at the end 最后%A2

\end{document}

This means that I can just run my analysis and (when I need to) create an external file that can be manipulated by Latex-based programs. 这意味着我可以运行我的分析并(当我需要时)创建一个可以由基于Latex的程序操作的外部文件。 I'm not interested in putting all the tables into one document but I want them separate (hence the need for \\begin{document} and \\end{document}. I've been experimenting with add.to.rows (xtable function) but I'm getting nowhere. 我对将所有表放入一个文档并不感兴趣,但我希望它们分开(因此需要\\ begin {document}和\\ end {document}。我一直在尝试使用add.to.rows(xtable函数)但我无处可去。

Example: 例:

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

How would you add both A1 and A2 so that the result is: 你如何添加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}

I got stuck right at the start: 我一开始就卡住了:

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)

I need \\end{document} to be right at the end but if I change 我需要\\ end {document}在最后但是如果我改变了

bottom$pos

to

bottom$pos[[1]] <- c(nrow(x)+3)

I get an error. 我收到一个错误。 I also haven't included the top part (A1- see above). 我也没有包括顶部(A1-见上文)。

Ideally I'd like the code to be as generic as possible so that it can be applied to any xtable output (eg anovas, sideway tables etc...). 理想情况下,我希望代码尽可能通用,以便它可以应用于任何xtable输出(例如anovas,sideway tables等...)。

Any ideas? 有任何想法吗?

Many thanks 非常感谢

The cat and print.xtable functions both have an 'append' argument: catprint.xtable函数都有一个'append'参数:

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM