首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在officer中插入自定义TOC的最佳方法

在officer中插入自定义TOC的最佳方法
EN

Stack Overflow用户
提问于 2021-10-31 19:58:42
回答 2查看 80关注 0票数 0

我需要将自定义目录插入到officer文档中。在这种情况下,我需要插入使用level 6标头创建的表列表。我需要插入的TOC字段是:

代码语言:javascript
复制
{TOC \o "6-6" \* MERGEFORMAT}

block_toc函数似乎不允许我这样做。所以我想要做的就是使用该函数中的内部函数(例如,run_seqfieldto_wml等)来实现这一点。我想知道是否有人有任何其他想法,利用更标准的军官职能。

在下面的示例中,我尝试使用block_toc的style选项创建一个表列表。当我运行这个命令时,它告诉我“没有找到目录条目”。

代码语言:javascript
复制
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")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-01 12:10:44

这应该会有帮助(还要记住从Word中刷新目录):

代码语言:javascript
复制
library(officer)
library(flextable)
library(magrittr)

mytxt <- paste(rep("The quick brown fox jumped over the lazy dog.", 30), collapse = " ")
long_cap <- "This is my table caption. It can span many lines and take up much space on the page."
tab_seq_id <- "Table"

ft <- flextable(head(mtcars))

get_caption <- function(bookmark){
  par <- list(
    ftext("Table "),
    run_word_field("STYLEREF 1 \\s"),
    ftext("-"),
    run_word_field("SEQ Table \u005C* Arabic")
  )
  run_bookmark(bookmark, par)
}


fptxt <- fpar(
  "This is a cross reference to the first table (",
  run_reference("my_table1"),
  ") and this is a reference to the second table (",
  run_reference("my_table2"), ").",
  ") and a third table in a new section (",
  run_reference("my_table3"), ")."
)


rpt <- read_docx() %>% 
  body_add_toc(style = "Table Caption") %>% 
  body_add_par("A section", style = "heading 1") %>% 
  body_add_par(value = mytxt, style = "Normal") %>% 
  body_add_par("Another section", style = "heading 1") %>% 
  body_add_fpar(fptxt) %>% 
  body_add_flextable(value = ft) %>% 
  body_add_fpar(value = fpar(get_caption(bookmark = "my_table1"), " ", long_cap), style = "Table Caption") %>% 
  body_add_flextable(value = ft) %>% 
  body_add_fpar(value = fpar(get_caption(bookmark = "my_table2"), " ", long_cap), style = "Table Caption") %>% 
  body_add_par("A third section", style = "heading 1") %>% 
  body_add_flextable(value = ft) %>% 
  body_add_fpar(value = fpar(get_caption(bookmark = "my_table3"), " ", long_cap), style = "Table Caption")
  


print(rpt, "fig_sec_num.docx")

票数 1
EN

Stack Overflow用户

发布于 2021-11-05 17:09:11

我需要在上面的body_add_toc()调用中添加separator=",“

我开始查看在指定Word样式时生成的TOC字段代码。David提供的示例在他的计算机上工作,但我无法让它在我这一端生成TOC。我开始使用生成的字段代码来尝试解决问题。在Davids solution中,officer为我生成的字段代码为:

代码语言:javascript
复制
{TOC \h \z \t "Table Caption;1"}

这对我不起作用。然后我发现,如果我将字段代码更改为以下其中之一,它将正确生成TOC:

代码语言:javascript
复制
{TOC \h \z \t "Table Caption"}
代码语言:javascript
复制
{TOC \h \z \t "Table Caption,1"}

在第一个示例中,我完全省略了";1“,在第二个示例中,我将";”改为“,”。所以我开始用谷歌搜索,如果我没看错的话,可能是某种区域设置导致了这一点:

https://answers.microsoft.com/en-us/msoffice/forum/all/table-of-contents-word-2016-word-2013/842156d0-51dd-4726-9c36-343e57bf9f92

然后,我阅读了body_add_toc的文档,并意识到有一个选项可以解决这个问题。我以前见过它,但我不知道它为什么会在那里,但这对我来说解决了一切。

谢谢你的耐心,大卫。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69790143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档