Repeat headers when using xtable with the longtable option

Is there a way to repeat the top row / set headers when creating an xxtable with the longtable option?

For example, if I have

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

This works fine, but when the tables jump to a new page, the headers are not repeated. Any suggestions?

+8
r latex xtable
source share
2 answers

To accomplish this, you need to use the add.to.row option of the add.to.row function (run ?print.xtable for more information).

Try this (adapted from 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 

This is a bit awkward solution, but at least it will provide you with lots of tweaks.

+18
source share

Looking at the code for print.xtable, the only considerations it makes when tabular.environment="longtable" are

  • A warning if you also set floating=TRUE
  • Put the signature in the right place for the long table (top or bottom)

It does not highlight code specific for repeating headings on subsequent pages. Check out latex in the Hmisc package. I know that it also supports longtables, but I don’t remember if it plays the headers correctly.

+2
source share

All Articles