kableExtra: Continued on Next Page for longtable
Asked Answered
E

1

9

I am using kableExtra for longtable with the following code.

library(knitr)
library(kableExtra)

long_dt <- rbind(mtcars, mtcars)

kable(
      long_dt, 
      format    = "latex", 
      longtable = T, 
      booktabs  = T, 
      caption   = "Longtable"
      ) %>%
add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) %>%
kable_styling(latex_options = c("repeat_header"))

The output is

enter image description here

I wonder how to add text (Continued on Next Page ...) at the bottom of the table if it spans to next page.

Enthronement answered 16/9, 2017 at 6:51 Comment(2)
I don't think you can do this using R code. The longtable LaTeX package supports it, but there's no way to request it using knitr or kableExtra functions.Zenas
Thanks @Zenas for your comment. I got the required output in R using xtable (See here).Enthronement
H
13

There is a kableExtra argument for that. I haven't checked but I suppose it wasn't available when the question was first posted.

From the documentation:

repeat_header_continued: T/F or a text string. Whether or not to put a continued mark on the second page of longtable. If you put in text, we will use this text as the "continued" mark.

The default is (continued...) so for your specific case it would be:

library(knitr)
library(kableExtra)

long_dt <- rbind(mtcars, mtcars)

kable(
      long_dt, 
      format    = "latex", 
      longtable = T, 
      booktabs  = T, 
      caption   = "Longtable"
      ) %>%
add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) %>%
kable_styling(latex_options = c("repeat_header"),
              repeat_header_continued = "\\textit{(Continued on Next Page...)}")

Output:

Howrah answered 7/6, 2020 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.