简体   繁体   中英

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. The output should be in 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.

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 " -- ". So the table caption in MSWord should look something like:

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:

---
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:

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

The latter actually is the way I found to change prefix word "Table" to "MyTable".

I also have the MSWord file with styles, made according to Pandoc user manuals. 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).

I have also tried to knit the output file without using 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.

My package versions are:

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. I ended using officedown package. For it the code of YAML will be:

---
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)
---

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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