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

<<label = tabggplot2df, echo = FALSE, results = tex >>=
           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
  , add.to.row = list(pos = list(0), command = "\\hline \\endhead ")

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

enter image description here
enter image description here

1 Answer 1


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) <- 
digits(xtab.table) <- 
   c(0, # the first one is for the row.names
display(xtab.table) <- 
   c("s", # the first one is for the row.names
align(xtab.table) <- 
   c("l", # the first one is for the row.names

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 ",
               "}{c}{{\\tablename\\ \\thetable{} -- continued from previous page}}\\\\ ",
               sep = ""),
         "\\toprule ",
         attr(xtab.table, "names")[1],
               attr(xtab.table, "names")[2:length(attr(xtab.table, "names"))],
               collapse = ""),
         "\\\\\\midrule ",
         "\\endhead ",
         "\\midrule ",
               "}{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).

      floating = FALSE, # longtable never floats
      hline.after = NULL,
      add.to.row = list(pos = list(-1, 
                        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.

  • 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
    Commented Jan 15, 2012 at 23:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .