问题描述
我希望能够使用官员 R 包对Word文档中的表格或图形进行交叉引用.
I would like to be able to cross-reference a table or figure in a word document using the officer R package.
到目前为止,我已经看过这些材料,但是它们似乎没有解决方案: https://davidgohel.github.io/legs/articles/word.html#table-and-image-captions 和一个类似的问题为docx中的flextable添加标题
I have come across these materials so far but they do not seem to have a solution:https://davidgohel.github.io/officer/articles/word.html#table-and-image-captionsand a similar questionadd caption to flextable in docx
在这两种方法中,我只能插入标题作为2级标题,而不能插入真正的表标题.
In both of these I can only insert a caption as a level 2 header and not a true table caption.
我要在Word中执行的操作是插入->交叉引用,然后转到引用类型:表格,然后在此处查看我的标题.现在,我只能在编号"项下看到标题.
What I want to be able to do in Word is Insert -> Cross-reference and go to Reference type: Table and see my caption there. Right now I can only see the caption under Numbered item.
此功能是否存在于管理人员或其他任何地方?
Does this functionality exist in officer or anywhere else?
推荐答案
换句话说,表编号使用{ SEQ \\@ arabic }
模式,但对它们的引用使用{ REF bookmark \h }
.我们可以使用它来创建可以引用SEQ字段的新代码.
In word, the table numbers use the { SEQ \\@ arabic }
pattern, but references to them use { REF bookmark \h }
. We can use this to make new code which can reference a SEQ field.
代码:
ft <- regulartable(head(iris)) # create flextable
str <- paste0(' REF ft \\h ') # create string to be used as reference to future bookmark
doc <- read_docx() %>%
body_add_par('This is my caption' , style = 'Normal') %>% # add caption
slip_in_seqfield(str = "SEQ Table \\@ arabic",
style = 'Default Paragraph Font',
pos = "before") %>% # add number for table
body_bookmark('ft') %>% # add bookmark on the number
slip_in_text("Table ",
style = 'Default Paragraph Font',
pos = "before") %>% # add the word 'table'
body_add_flextable(value = ft, align = 'left') %>% # add flextable
body_add_break() %>% # insert a break (optional)
slip_in_text('As you can see in Table',
style = 'Default Paragraph Font',
pos = 'after') %>% # add the text you want before the table reference
slip_in_seqfield(str = str,
style = 'Default Paragraph Font',
pos = 'after') %>% # add the reference to the table you just added
slip_in_text(', there are a lot of iris flowers.',
style = 'Default Paragraph Font',
pos = 'after') %>% # add the rest of the text
print('Iris_test.docx') # print
希望这会有所帮助:)
这篇关于表和图交叉引用官R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!