简体   繁体   中英

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

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...

Rather than using a loop, consider using the existing print.xtable functionality combined with paste.

  1. hdashlines: Consider using the print.xtable parameters. For example, there is a parameter hline.after that controls horizontal lines between the table lines.

  2. vspace: This is possibly simpler using 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
\\hline\\n\\end{tabular}\\n\\end{center}\\n\\end{table}\\n \\vspace{2em}"


See ?print.xtable for more details. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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