How to put a "continued on next page" at the page bottom of a longtable is described in the documentation of longtable. Instead of telling you to read the manual, I will show you my code, which solves these issues:
xtab.table <- xtable('your dataframe')
names(xtab.table) <-
c("{Col1}",
"{Col2}",
"{Col3")
digits(xtab.table) <-
c(0, # the first one is for the row.names
0,
0,
0)
display(xtab.table) <-
c("s", # the first one is for the row.names
"s",
"s",
"s")
align(xtab.table) <-
c("l", # the first one is for the row.names
"r",
"l",
"l")
The xtable
documentation in R describes the digits, display and align commands. These are invaluable when you try to make more complex tables, for example they are very useful in combination with siunitx
table formats.
table.caption <- "Your table's caption."
table.label <- "tab:label"
longtable.header <-
paste(paste("\\caption{", table.caption, "}",
sep = "", collapse = ""),
paste("\\label{", table.label, "}\\\\ ",
sep = "", collapse = ""),
"\\toprule ",
attr(xtab.table, "names")[1],
paste(" &",
attr(xtab.table, "names")[2:length(attr(xtab.table, "names"))],
collapse = ""),
"\\\\\\midrule ",
"\\endfirsthead ",
paste("\\multicolumn{",
ncol(xtab.table),
"}{c}{{\\tablename\\ \\thetable{} -- continued from previous page}}\\\\ ",
sep = ""),
"\\toprule ",
attr(xtab.table, "names")[1],
paste("&",
attr(xtab.table, "names")[2:length(attr(xtab.table, "names"))],
collapse = ""),
"\\\\\\midrule ",
"\\endhead ",
"\\midrule ",
paste("\\multicolumn{",
as.character(ncol(xtab.table)),
"}{r}{{Continued on next page}}\\\\ ",
sep = "", collapse = ""),
"\\bottomrule \\endfoot ",
"\\bottomrule \\endlastfoot ",
collapse = "")
When I need to print()
the xtable, the following works pretty well, and also removes that pesky extra bottomrule (note that I use the booktabs package (which uses \toprule
, \midrule
and \bottomrule
instead of \hline
, but you just replace those rule commands with hline if you don't fancy booktabs).
print(xtab.xtable,
floating = FALSE, # longtable never floats
hline.after = NULL,
add.to.row = list(pos = list(-1,
nrow(xtab.table)),
command = c(longtable.header,
"%")), # note the percent sign
# It will cause that trailing \hline
# command in your .tex file to be
# commented out. Not in any manual I've
# seen, by the way. Just a trick I use.
include.rownames = FALSE,
include.colnames = FALSE,
type = "latex",
tabular.environment = "longtable",
sanitize.text.function = function(x){x},
math.style.negative = FALSE)
These code snippets contain all you need to solve your issues (plus some). I hope it's not too overwhelming. As you may notice, the longtable.header
string is quite complicated, mainly because I needed to incorporate the nicer-looking horizontal rules of booktabs. longtable.header
can be significantly simplified if \hline
is used instead, although \hline
looks awful in comparison to proper rules, in my opinion.