【问题标题】:Adding a horizontal line between the rows in a latex table using R xtable使用 R xtable 在乳胶表中的行之间添加水平线
【发布时间】:2011-08-23 12:04:53
【问题描述】:

我有一个像这样的示例乳胶表生成代码

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)

现在我想在每个表的每一行之后自动引入 \hdashline,并且在 R 代码的表之间还有一个 \vspace{2em} 我试过的就是这个

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

我需要在 for 循环中进行哪些更改..当我申请单个表但不在 for 循环中工作时它可以工作...非常感谢任何帮助...

【问题讨论】:

  • +1 用于发布示例数据和代码。欢迎来到 SO。

标签: r latex xtable


【解决方案1】:

考虑使用现有的print.xtable 功能与粘贴相结合,而不是使用循环。

  1. hdashlines:考虑使用print.xtable 参数。比如有一个参数hline.after控制表格行之间的水平线。

  2. vspace:使用paste 可能更简单。

例如:

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 在 R 2.13.1 中由 xtable 1.5-6 包生成\n% 8 月 23 日星期二 13:36:41 2011\n\begin{table}[ht]\n\begin{center}\n\begin{tabular}{rrrrrrrr}\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}"


有关详细信息,请参阅?print.xtable。如果hline.after 对您的目的不够灵活,请查看add.to.row,您可以在其中指定要添加的位置和确切的元素类型。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      相关资源
      最近更新 更多