简体   繁体   English

如何使用 bookdown 更改 RMarkdown 中表格标题中的前缀词和分隔符?

[英]How to change the prefix word and separator in table captions in RMarkdown with bookdown?

I am writing a document in RStudio using RMarkdown, knitr and R scripts.我正在使用 RMarkdown、 knitr和 R 脚本在 RStudio 中编写文档。 The output should be in MSWord.输出应为 MSWord。 My document contains a lot of tables, so I need to have neatly formatted talble titles and cross reffernces to them.我的文档包含很多表格,所以我需要格式整齐的表格标题和它们的交叉引用。 The only way to have cross references I found was bookdown package, so I use it to knit Word document from RMarkdown.我发现交叉引用的唯一方法是bookdown包,所以我用它从 RMarkdown 编织 Word 文档。

My problem is I cannot change prefix word and separator in the table caption.我的问题是我无法更改表格标题中的前缀词和分隔符。 For example, I need the prefix word to be "MyTable" and separator " -- ".例如,我需要前缀词是“MyTable”和分隔符“--”。 So the table caption in MSWord should look something like:所以 MSWord 中的表格标题应该类似于:

MyTable 1.1 -- Table caption

| Col1 | Col2 | Col3 |
----------------------
| Text | Text | Text |

But what I finally managed to get is only:但我最终设法得到的只是:

MyTable 1.1: Table caption

| Col1 | Col2 | Col3 |
----------------------
| Text | Text | Text |

The main problem is to replace ":" witn "--".主要问题是用“--”替换“:”。

My code in RMarkdown is as follows:我在 RMarkdown 中的代码如下:

---
title: "Test"
output:
  bookdown::word_document2:
    reference_docx: template.docx
  html_document: default
---

.```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(tab.cap.pre = "MyTable ", tab.cap.sep = " -- ")
library(bookdown)
library(flextable)
set_flextable_defaults(font.size = 11, font.family = "Times New Roman", decimal.mark = ",", big.mark = " ", theme_fun = "theme_box")
.```

# Chapter Header

Some paragraphs of text. Then goes comes the place for my table.

.```{r}
ft <- flextable(head(mtcars))
ft <- set_caption(ft, "My table caption", autonum = run_autonum(seq_id = "tab", post_label = " -- ", bkm = "tab1"))
ft
.```

Very important information is in MyTable \@ref(tab:tab1).

Besides I have _bookdown.yml file with the following language settings:此外,我还有具有以下语言设置的 _bookdown.yml 文件:

language:
  label:
    fig: 'MyPicture '
    tab: 'MyTable '

The latter actually is the way I found to change prefix word "Table" to "MyTable".后者实际上是我发现将前缀词“Table”更改为“MyTable”的方法。

I also have the MSWord file with styles, made according to Pandoc user manuals.我还有带样式的 MSWord 文件,是根据 Pandoc 用户手册制作的。 In the output I get my caption formatted according to the style it should be (but the word "Table" and number is not formatted as it would be if the table caption were made in MSWord).在输出中,我根据应有的样式对标题进行了格式化(但是“表格”一词和数字的格式与表格标题是在 MSWord 中制作的格式不同)。

I have also tried to knit the output file without using bookdown .我还尝试在不使用bookdown的情况下编织输出文件。 In this case the line: knitr::opts_chunk$set(tab.cap.pre = "MyTable ", tab.cap.sep = " -- ") seems to work, I get the right prefix word and the right separator, but simple table number (the result is "MyTable 1" instead of "MyTable 1.1") and sure no cross references.在这种情况下,行: knitr::opts_chunk$set(tab.cap.pre = "MyTable ", tab.cap.sep = " -- ")似乎有效,我得到了正确的前缀词和正确的分隔符,但是简单的表编号(结果是“MyTable 1”而不是“MyTable 1.1”)并且确保没有交叉引用。

My package versions are:我的包版本是:

bookdown - 0.27 flextable - 0.7.3 knitr - 1.3.8 officer - 0.4.3 bookdown - 0.27 flextable - 0.7.3 knitr - 1.3.8 officer - 0.4.3

Unfortunately, I did not have any success with lua filters.不幸的是,我没有成功使用 lua 过滤器。 I ended using officedown package.我结束了使用officedown包。 For it the code of YAML will be:对于它,YAML 的代码将是:

---
title: "Test"
output:
  officedown::rdocx_document:
    reference_docx: template.docx
    tables:
      style: Table
      caption:
        pre: "Table "
        sep: " -- "
# To prevent word "Table" and crossref numbers from being bold
        fp_text: !expr officer::fp_text_lite(bold = FALSE)
---

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM