4

I want to have longtable spanning several pages with word continued at right bottom of the table and its caption appearing only one in the list of tables using knitr. Below is my MWE in .Rnw format. Any help will be highly appreciated. Thanks in advance for your help.

\documentclass{article} 
\usepackage{longtable}

\begin{document}

\listoftables



<< label=LongTable, results='asis', echo = FALSE >>=
library(xtable)
set.seed(12345)
MatrixData <- matrix(rnorm(1000), ncol = 10)

print(
  xtable(
      x = MatrixData
    , caption = "Example of longtable spanning several pages"
    , label = "tab:MatrixData"
    , align = c("l|", rep("r", ncol(MatrixData)))
    , digits = c(rep(3, ncol(MatrixData)+1))
  )

  , table.placement = "H"
  , caption.placement = "top"
  , include.rownames = TRUE
  , include.colnames = TRUE
  , size = "small"
  , tabular.environment = 'longtable'
  , floating = FALSE
  , add.to.row = list(pos = list(0),command = "\\hline \\endhead ")
)
@

\end{document}

Output

enter image description here enter image description here enter image description here

6

I think this will do the trick:

\documentclass{article} 
\usepackage{longtable}

\begin{document}

\listoftables

<< label=LongTable, results='asis', echo = FALSE >>=
library(xtable)
set.seed(12345)
MatrixData <- matrix(rnorm(1000), ncol = 10)

print(
  xtable(
      x = MatrixData
    , caption = "Example of longtable spanning several pages"
    #, label = "tab:MatrixData"
    , align = c("l|", rep("r", ncol(MatrixData)))
    , digits = c(rep(3, ncol(MatrixData)+1))
  )

  , table.placement = "H"
  , caption.placement = "top"
  , include.rownames = TRUE
  , include.colnames = TRUE
  , size = "small"
  , tabular.environment = 'longtable'
  , floating = FALSE
  , add.to.row = list(pos = list(0),command = 
                        paste("\\hline  \\endfirsthead"  ,                          # First caption
                                "\\caption[]{Example of longtable spanning several pages} \\label{tab:MatrixData} \\\\ \\hline", # Additional captions
                                paste("&", 1:ncol(MatrixData),collapse=" "),                              # Column names
                                "\\\\ \\hline ",
                                "\\endhead", 
                                "\\hline \\multicolumn{11}{r}{\\textit{Continued}} \\                    
                                 \\endfoot
                                 \\endlastfoot",collapse=" ")))
@

 \end{document}
7
  • Thanks for your answer. Your answer doesn't repeat the header row. Any idea. – MYaseen208 Jan 6 '15 at 14:08
  • Also included the word Continued in the footer now – user1570707 Jan 6 '15 at 14:10
  • (+1) Great. But still header row is not repeated on each page. – MYaseen208 Jan 6 '15 at 14:13
  • I didn't realise that the names of the columns were removed. The edited code should work but this is probably not the easiest solution. – user1570707 Jan 6 '15 at 15:36
  • 1
    You are welcome! The problem with the vertical line on the last page is caused by the label. xtable puts the label after the table but before \end{longtable}. An alternative is to not define label and to instead add the \label to the add.to.row command, as described above. – user1570707 Jan 6 '15 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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