简体   繁体   English

使用R xtable在乳胶表中的行之间添加水平线

[英]Adding a horizontal line between the rows in a latex table using R xtable

I have a sample latex table generation code like this 我有这样的乳胶表生成代码示例

df<-data.frame(name=rep(letters[1:7],each=24),salary=runif(24*7,100,200))
lst<-tapply(df$salary,df$name,matrix,nrow=4,byrow=T)
xlst<-lapply(lst,xtable)

Now I want to introduce \\hdashline automatically after every row in every table and also a \\vspace{2em} in between the tables from R code Wat I have tried is this 现在我想在每个表中的每一行之后自动引入\\ hdashline,并且在R代码之间的表之间还有一个\\ vspace {2em} Wat我试过这个

for(i in seq_along(lst)){
  addtorow[i] <- list(pos = list(seq_len(nrow(lst[[i]])-1)),
                      command = "\\hdashline \n")
}

wat changes do i need to make in for loop..It works when I apply for a single table but not working in a for loop...Any help is much appreciated... 扫描更改我需要进行循环..它适用于我申请单个表但不能在for循环中工作...任何帮助非常感谢...

Rather than using a loop, consider using the existing print.xtable functionality combined with paste. 考虑使用现有的print.xtable功能与粘贴相结合,而不是使用循环。

  1. hdashlines: Consider using the print.xtable parameters. hdashlines:考虑使用print.xtable参数。 For example, there is a parameter hline.after that controls horizontal lines between the table lines. 例如,有一个参数hline.after控制表格行之间的水平线。

  2. vspace: This is possibly simpler using paste . vspace:使用paste可能更简单。

For example: 例如:

library(xtable)
df <- data.frame(name=rep(letters[1:3],each=24),salary=runif(24*3,100,200))
lst <- tapply(df$salary,df$name,matrix,nrow=4,byrow=T)
xlst <- lapply(lst,function(x)print(xtable(x), hline.after=1:nrow(x)))
xlst <- lapply(xlst, paste, "\\vspace{2em}")
xlst

[1] "% latex table generated in R 2.13.1 by xtable 1.5-6 package\\n% Tue Aug 23 13:36:41 2011\\n\\begin{table}[ht]\\n\\begin{center}\\n\\begin{tabular}{rrrrrrr}\\n & 1 & 2 & 3 & 4 & 5 & 6 \\\\ \\n 1 & 158.66 & 115.81 & 106.70 & 128.78 & 157.43 & 191.01 \\\\ \\n \\hline\\n2 & 159.09 & 172.31 & 153.93 & 127.91 & 106.93 & 147.95 \\\\ \\n \\hline\\n3 & 135.65 & 139.45 & 192.90 & 108.78 & 186.52 & 164.10 \\\\ \\n \\hline\\n4 & 190.10 & 154.39 & 124.91 & 199.24 & 161.99 & 167.61 \\\\ \\n [1]“通过xtable 1.5-6包在R 2.13.1中生成的%乳胶表\\ n%Tue Aug 23 13:36:41 2011 \\ n \\ begin {table} [ht] \\ n \\ begin {center} \\ n \\ begin {tabular} {rrrrrr} \\ n&1&2&3&4&5&6 \\\\ \\ n 1&158.66&115.81&106.70&128.78&157.43&191.01 \\\\ \\ n \\ hline \\ n2&159.09& 172.31&153.93&127.91&106.93&147.95 \\\\ \\ n \\ hline \\ n3&135.65&139.45&192.90&108.78&186.52&164.10 \\\\ \\ n \\ hline \\ n4&190.10&154.39&124.91&199.24&161.99&167.61 \\ \\ n
\\hline\\n\\end{tabular}\\n\\end{center}\\n\\end{table}\\n \\vspace{2em}" \\ hline \\ n \\ end {tabular} \\ n \\ end {center} \\ n \\ end {table} \\ n \\ vspace {2em}“


See ?print.xtable for more details. 有关详细信息,请参阅?print.xtable If hline.after is not flexible enough for your purposes, then have a look at add.to.row where you can specify both the position and exact element type to add. 如果hline.after对于您的目的不够灵活,那么请查看add.to.row ,您可以在其中指定要添加的位置和确切元素类型。

如果您使用的是print.xtable ,请在print.xtable中尝试以下print.xtable

hline.after = rep(c(1:nrow(df)),2)# repeating horizontal lines in xtable

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

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