【问题标题】:Repeat headers when using xtable with longtable option使用带有 longtable 选项的 xtable 时重复标题
【发布时间】:2011-09-16 20:38:01
【问题描述】:

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

例如,如果我有

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

这很好用,但是当表格滚动到新页面时,标题不会重复。有什么建议吗?

【问题讨论】:

标签: r latex xtable longtable


【解决方案1】:

为了完成此操作,您需要使用print 函数的add.to.row 选项(运行?print.xtable 了解更多信息)。

试试这个(改编自a post on R-Forge

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

这是一个有点笨拙的解决方案,但至少它会为您提供大量的自定义。

【讨论】:

  • print.xtable 中的 floating=FALSE 语句是否绝对必要?我想要一个能够滚动到新页面并处于横向稳定环境中的 xtable,因此它是横向的?
  • @gvrocha:如果我没记错的话,你是对的。 floating 可以设置为TRUE 没有问题。
  • 至少对于 xtable 1.7-4,floating 设置为 FALSE 并自动发出警告。因此,似乎没有办法将longtable 封装在另一个环境中(另见this feature request)。
【解决方案2】:

查看 print.xtable 的代码,它在tabular.environment="longtable" 时所做的唯一考虑是

  • 如果您还设置了floating=TRUE,则会发出警告
  • 将标题放在长桌(顶部或底部)的正确位置

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

【讨论】:

  • 具体来说,您正在寻找一种方法来将列标题后面的换行符 (\) 替换为 \endhead。我没有看到该选项,因此可能需要手动完成。
猜你喜欢
  • 2014-11-08
  • 2016-07-28
  • 2015-09-23
  • 2015-11-22
  • 1970-01-01
  • 2022-11-19
  • 2021-12-14
  • 1970-01-01
  • 2011-07-30
相关资源
最近更新 更多