如何在R中使用官员在Word文档的标题中放置下标

问题描述

我正在尝试为在官员中生成标题添加下标。 我正在创建这样的标题

library(officer)

doc <- read_docx('empty_file.docx')

autonum <- run_autonum(seq_id = 'fig',pre_label = 'figure ')
caption <- block_caption(label='My caption includes SO2.',style = "caption",autonum = autonum)
doc <- body_add_caption(doc,caption)

print(doc,target = 'output.docx'))

但是,现在我想将“SO2”中的“2”放在下标中。我知道如何生成下标:

fp_text_prop <- fp_text(color='orange')
prop_mod <- update(fp_text_prop,vertical.align = 'subscript')
paragraph <- fpar(ftext('SO',prop = fp_text_prop),ftext('2',prop = prop_mod)))

但我不能在标题中使用生成fpar,因为 body_add_caption 需要来自 block_caption输出,而 block_caption 需要一个普通字符串作为 { {1}}。

如何在标题添加 label= 或下标?

解决方法

我找到了一个有点复杂但似乎有效的解决方案。

library(officer)

doc <- read_docx('empty_file.docx')

autonum <- run_autonum(seq_id = 'fig',pre_label = 'Figure ')

fp_text_prop <- fp_text(color='orange')
prop_mod <- update(fp_text_prop,vertical.align = 'subscript')

caption <- fpar(autonum,ftext('SO',prop = fp_text_prop),ftext('2',prop = prop_mod)))

doc <- body_add_fpar(x=doc,value=caption,style = 'caption')

print(doc,target = 'output.docx'))

有几个注意事项:fp_text_prop 应与标题的正常样式相匹配,而 style = 'caption' 应更改为为文档中的标题选择正确的样式。