Skip to content

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

Description

@ujtwr

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


Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions