使用带有longtable选项的xtable时重复标题

时间:2011-09-16 20:38:01

标签: r latex xtable longtable

在使用longtable选项生成xtable时,有没有办法重复顶行/设置标题?

例如,如果我有

tableSb <- xtable(df, caption="A Very Long Table", label="ALongTable")
print(tableSb, include.rownames=TRUE, tabular.environment="longtable", floating=FALSE)

这样可以正常工作,但是当表格滚动到新页面时,标题不会重复。有什么建议吗?

2 个答案:

答案 0 :(得分:21)

为了实现这一目标,您需要使用add.to.row函数的print选项(运行?print.xtable以获取更多信息)。

试试这个(改编自https://r-forge.r-project.org/tracker/?func=detail&atid=4864&aid=1627&group_id=1228

addtorow          <- list()
addtorow$pos      <- list()
addtorow$pos[[1]] <- c(0)
addtorow$command  <- c(paste("\\hline \n",
                             "\\endhead \n",
                             "\\hline \n",
                             "{\\footnotesize Continued on next page} \n",
                             "\\endfoot \n",
                             "\\endlastfoot \n",sep=""))
x.big <- xtable(x, label = "tabbig", caption = "Example of longtable spanning several pages")
print(x.big, tabular.environment = "longtable", floating = FALSE,
      include.rownames = FALSE,  # because addtorow will substitute the default row names 
      add.to.row = addtorow,     # this is where you actually make the substitution
      hline.after=c(-1))         # because addtorow will substitute the default hline for the first row

解决方案有点笨拙,但至少它会为您提供充足的自定义。

答案 1 :(得分:2)

查看print.xtable的代码,tabular.environment="longtable"

时唯一考虑的因素
  • 如果您还设置了floating=TRUE
  • ,则会发出警告
  • 将标题放在正确的位置(顶部或底部)

它不会发出特定于在后续页面上重复标题的代码。查看latex包中的Hmisc。我知道它也支持longtables,但我不记得它是否正确重复了标题。

相关问题