Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add caption to flextable in docx

How could one add a caption to a flextable rendered to docx? EDIT: The aim is to produce a proper caption which can be referenced within the document to produce a list of tables and inline references.

iris.t <-
  iris[1:5,] %>%
  regulartable() %>% 
  style(pr_c = officer::fp_cell(vertical.align = "bottom",
                                border.bottom = officer::fp_border(width = 2)), part = "header") %>% 
  rotate(j = names(iris)[-c(1:2)],
         rotation = "tbrl", part = "header", align = "bottom") %>% 
  height(height = max(dim_pretty(., part = "header")$widths), part = "header") %>% 
  width(width = dim_pretty(.,part = "body")$widths)

iris.t 
like image 613
JAllen Avatar asked Apr 05 '18 16:04

JAllen


1 Answers

I use the following way to caption tables.

#set the table caption styling
knitr::opts_chunk$set(tab.cap.pre = "Table ", tab.cap.sep = ": ")

#set the table caption styling
set_flextable_defaults(font.family = "Calibri (Body)",
                       font.size = 9, 
                       digits = 0, 
                       border.color = "#000000",
                       padding.bottom = 1,
                       padding.top = 1,
                       padding.left = 3,
                       padding.right = 1)

ft <- flextable(df, defaults = TRUE) #convert to flextable object
autonum <- run_autonum(seq_id = "tab", bkm = "TC1", bkm_all = TRUE) # number the table, bkm (bookmark) is important as the cross-referencing is done using the bookmark
ft <- set_caption(ft, caption = "Traffic Counts for Existing Condition", 
                  style = "Table Caption", autonum = autonum)
ft # to print the table

to cross-reference the table use \@ref(tab:TC1)

to produce a list of tables use

<!---BLOCK_TOC{seq_id: 'tab'}--->
like image 159
naveen chandra Avatar answered Oct 31 '22 22:10

naveen chandra