Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
  , 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

share|improve this question
add comment

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) <- 
   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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.