I need to insert a custom TOC into an officer
document. In this case I need to insert a list of tables that are created using the level 6 headers. The TOC field I need to insert is:
{TOC \o "6-6" \* MERGEFORMAT}
The block_toc
function doesn't seem to allow me to do this. So was thinking of doing is using the internal functions within that function (e.g., run_seqfield
, to_wml
, etc) to accomplish this. I was wondering if anyone had any other ideas that utilized the more standard officer functions.
In the example below I'm trying to create a list of tables using the style option with block_toc. When I run this it gives me "No table of contents entries found."
library(officer)
library(flextable)
library(magrittr)
tab_seq_id = "Table"
# empty report
rpt = read_docx()
bt <- block_toc(style = "Table Caption")
out <- to_wml(bt, add_ns = TRUE)
rpt <- body_add_xml(rpt, str = out, pos = "after")
mytxt = paste(rep("The quick brown fox jumped over the lazy dog.", 30), collapse=" ")
# Making a table
ft = flextable(head(mtcars))
# Creating some sections with text
rpt = body_add_fpar(rpt, fpar("A section"), style="heading 1")
fptxt = fpar(mytxt)
rpt = officer::body_add_fpar(rpt, fptxt)
rpt = body_add_fpar(rpt, fpar("Another section"), style="heading 1")
fptxt = fpar("This is a cross reference to the first table (Table ",
run_reference("my_table"),
") and this is a reference to the second table (Table ",
run_reference("my_second_table"), ")." ,
") and a third table in a new section (Table ",
run_reference("my_third_table"), ")." )
long_cap = "This is my table caption. It can span many lines and take up much space on the page."
#-------------------------------------------------------
# Normal table
run_num = officer::run_autonum(seq_id = tab_seq_id,
pre_label = "Table ",
post_label = ".",
bkm = "my_table")
caption = officer::block_caption(long_cap,
style = "Table Caption",
autonum = run_num )
rpt = officer::body_add_fpar(rpt, fptxt)
rpt = flextable::body_add_flextable(rpt, value=ft)
rpt = officer::body_add_caption(rpt, caption)
#-------------------------------------------------------
# Table with the section number in it
runs = list(
run_word_field("STYLEREF 1 \\s"),
ftext("-"),
officer::run_autonum(pre_label = "", seq_id = tab_seq_id, post_label=""))
rb_res = run_bookmark("my_second_table", runs)
rpt = flextable::body_add_flextable(rpt, value=ft)
rpt = officer::body_add_fpar(rpt, fpar("Table ", rb_res, ". ", long_cap), style = "Table Caption")
# Creating some sections with text
rpt = body_add_fpar(rpt, fpar("A third section"), style="heading 1")
#-------------------------------------------------------
# Table with the section number in it
runs = list(
run_word_field("STYLEREF 1 \\s"),
ftext("-"),
officer::run_autonum(pre_label = "", seq_id = tab_seq_id, start_at = 1, post_label=""))
rb_res = run_bookmark("my_third_table", runs)
rpt = flextable::body_add_flextable(rpt, value=ft)
rpt = officer::body_add_fpar(rpt, fpar("Table ", rb_res, ". ", long_cap), style = "Table Caption")
print(rpt, "fig_sec_num.docx")