# Caption for longtable in Sweave

I'm using the following code in .Rnw file to generate a longtable

<<label = tabggplot2df, echo = FALSE, results = tex >>=
print(
xtable(
x = ggplot2df
, caption = "Functions in ggplot2"
, label = "tab:ggplot2df"
, align = "r|l|l"
)
, table.placement = "H"
, caption.placement = "top"
, include.rownames = TRUE
, include.colnames = TRUE
, size = "footnotesize"
, hline.after = c(-1, nrow(ggplot2df))
, tabular.environment="longtable"
, floating=FALSE
)
@


I want to put something like this Table 1.1 (Continued ...) Caption after the first page an bottom line at each page. My code is generating an extra bottom line for the last page and one extra vertical line. I'd highly appreciate if you help me to figure out these issues. Thanks

-

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"
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 ",
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 ",
"\\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,
nrow(xtab.table)),
"%")), # 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.

-
Nice answer. And welcome to TeX.sx. We get quite a few Sweave questions here, but not as many people who are skilled in it, so feel free to answer more of them! –  Alan Munn Jan 15 '12 at 23:28