davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
553 stars 80 forks source link

Add extra header second page and after in Tables that span multiple pages in PDF document #394

Open ujtwr opened 2 years ago

ujtwr commented 2 years ago

When outputting PDF, flextable uses latex's "longtable". longtable allows for separate headers for the first page and the second and subsequent pages. I want to add text like "(continued)" at the top of the header for the second and subsequent pages. For this purpose,

I created the following function. Simply add text to the bottom of the latex source "\endfirsthead". It would be nice to be able to set any text, adjust font family, size, text alignment, etc. It might be nice if the footer could also be displayed on a page-by-page basis.

MyFunc <- function(x){
  tableNcol <- ncol_keys(x)

  if(!is.null(knitr::opts_knit$get("rmarkdown.pandoc.to"))){
    if(knitr::opts_knit$get("rmarkdown.pandoc.to") %in% c("beamer", "latex")) {
      flextable_to_rmd(x, print = FALSE) %>% 
        str_replace_all(
          fixed("\\endfirsthead"),
          fixed(str_glue("\\endfirsthead\n\n\\multicolumn{tableNcol}{{r}}{{(continued)}}\\\\", tableNcol=tableNcol))
        ) %>% 
        cat()
    }
  }else{
    return(x)
  }
}

If you use the following in a chunk, it will be rendered as is inline, and when knit, the latex source will be rewritten.

airquality %>% 
  slice(1:60) %>% 
  flextable() %>% 
  theme_booktabs() %>% 
  autofit() %>% 
  MyFunc()
davidgohel commented 2 years ago

Sorry for the delay

We will try, but no promise :)

In general, we are trying to add the same features for Word/PPTX/HTML/PDF but there it is a request for an exception for PDF only.

MargaretSiple-NOAA commented 1 year ago

I would also love to see this feature in the Word files generated with flextable. A header on the following page ("Table 1 continued"), footer on the first page ("Table 1 continues on following page") or similar would be really helpful for some of the reports I've been using flextable for. I will continue searching to see if this type of thing is already out there for the docx people.

davidgohel commented 1 year ago

Unfortunately this feature does not exist in MS Word.