如何更改bookdown中的图形标题格式

How to change the figure caption format in bookdown

当使用 bookdown(单个文档)时,数字会自动编号为:

Figure 1 Text of figure caption.

在化学中,惯例是将主要图形标记为:

Figure 1. Text of figure caption.

以及支持信息文件:

Figure S1. Text of figure caption.

另外在正文中参考我们需要的图:

...as can be seen in Figure 1, ...

所以参考文本不应该是粗体。

问题

我怎样才能使 bookdown(或 rmarkdown)生成像这样的图和 table 标题:“图 S1。 一些文本。”和“Table S1. 一些文本。” ?

我需要它采用 MS Word 格式。

示例/尝试的解决方案

到目前为止,我尝试按如下方式修改 _bookdown.yml 文档:

language:
  label:
    fig: "**Figure S**"
    tab: "**Table S**"

这给出:图 S1 一些文本...以及使用时的内嵌引用:

Figure S\@ref(fig:Xray)

是图S1,没问题

这是一个最小的例子:

---
title: Supporting Information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
  bookdown::word_document2:
    fig_caption: yes
    toc: yes
    toc_depth: 1
---

## Reaction of etc.
Some text  (Figure S\@ref(fig:Xray)). Some text followed by a figure:

```{r Xray, fig.cap="Single-crystal X-ray structure of some text", echo=FALSE}
plot(cars)
```
Some text etc. followed by a table:

```{r DipUVvis, echo=FALSE, tab.cap="Table caption"}
df<-data.frame(Entry=c('AMM 51^a$','AMM 52^*a*^'),
               Precat=c('[FeBr~2~(dpbz)~2~] (4.00)','[FeBr~2~(dpbz)~2~] 
(2.00)'))

kable(head(df), format = "markdown")
```

上面的代码在图标题中生成 图 S1 但不是 图 S1。(请注意,它全部为粗体且完整最后停止)。

据我所知,您无法通过 rmarkdown/bookdown 控制 figure/table 字幕来执行您想要的操作。您可以使用包 captioner 来实现它,但有一个问题:它可以很好地处理 rmarkdown 输出,但是您需要对 post-processing 进行 bookdown 输出。这是生成所需字幕样式的 Rmd 文件:

---
title: Supporting Information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
  word_document:
    fig_caption: yes
---

```{r, include=F}
library(captioner)
tables <- captioner(prefix = "Table S", suffix = ". ", style="b", style_prefix=TRUE, auto_space = FALSE)
figures <- captioner(prefix = "Figure S", suffix = ". ", style="b", style_prefix=TRUE, auto_space = FALSE)

figures("Xray1", "Single-crystal X-ray structure of some text (1)", display=FALSE)
figures("Xray2", "Single-crystal X-ray structure of some text (2)", display=FALSE)
figures("Xray3", "Single-crystal X-ray structure of some text (3)", display=FALSE)
```

## Reaction of etc.
Some text. Some text followed by `r figures("Xray1", display="cite")`, which is the same figure as  `r figures("Xray3", display="cite")` but comes after `r figures("Xray2", display="cite")`.

```{r Xray, fig.cap=figures("Xray1"), echo=FALSE}
plot(cars)
```

```{r Xray2, fig.cap=figures("Xray2"), echo=FALSE}
plot(cars)
```

```{r Xray3, fig.cap=figures("Xray3"), echo=FALSE}
plot(cars)
```

Some text etc. followed by `r tables("tab-DipUVvis", display="cite")`:

```{r DipUVvis, echo=FALSE}
df<-data.frame(Entry=c('AMM 51^a$','AMM 52^*a*^'),
               Precat=c('[FeBr~2~(dpbz)~2~] (4.00)','[FeBr~2~(dpbz)~2~] 
(2.00)'))

knitr::kable(head(df), caption=tables("tab-DipUVvis", "Table Caption"))
```

但是,如果您切换到使用 bookdown::word_document2 输出,图形标题将变为 "Figure 1: Figure S1. ..."。我还没有找到一种方法来抑制 "Figure 1:" 并且必须在我的输出中执行 post-processing 以搜索并将所有 "Figure ?:" 替换为 "".

this guide to set Word (.docx) style之后,您可以制作无花果。和选项卡。字幕粗体,尽管整个字幕行可以是粗体...我的意思是我们有一种方法可以通过 RMarkdown 在 .docx 中自动创建字幕:

Figure S1: Single-crystal X-ray structure of some text (1)

然而,做一个这样的似乎还是很难:

Figure S1: Single-crystal X-ray structure of some text (1)

我想您只想将“Figure/Table S1”部分加粗,而不是整个标题行。不过,如果您有兴趣使用 Rmarkdown 格式化 .docx 文件,您可以查看我在上面添加的 link 并查看以下说明。

1。编织 the .Rmd file @LmW。为我们提供了第一个 .docx 输出。

如果您对 captioner 包有一些问题,您也可以使用以下包。

---
title: Supporting Information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
  word_document:
     fig_caption: yes
---

```{r, include=F}
library(captioner)
#`captioner` package (Ver. 2.2.3) in my envionment returns the following error messages:
#Error in captioner(prefix = "Table S", suffix = ". ", style = "b", style_prefix = TRUE,  : 
#  unused arguments (suffix = ". ", style = "b", style_prefix = TRUE)

#Error in captioner(prefix = "Figure S", suffix = ". ", style = "b", style_prefix =     TRUE,  : 
#  unused arguments (suffix = ". ", style = "b", style_prefix = TRUE)

#tables <- captioner(prefix = "Table S", suffix = ". ", style="b", style_prefix=TRUE, auto_space = FALSE)
#figures <- captioner(prefix = "Figure S", suffix = ". ", style="b", style_prefix=TRUE, auto_space = FALSE)

tables <- captioner(prefix = "Table S",  auto_space = FALSE)
figures <- captioner(prefix = "Figure S", auto_space = FALSE)

figures("Xray1", "Single-crystal X-ray structure of some text (1)", display=FALSE)
figures("Xray2", "Single-crystal X-ray structure of some text (2)", display=FALSE)
figures("Xray3", "Single-crystal X-ray structure of some text (3)", display=FALSE)
```

## Reaction of etc.
Some text. Some text followed by `r figures("Xray1", display="cite")`, which is the same figure as  `r figures("Xray3", display="cite")` but comes after `r figures("Xray2", display="cite")`.

```{r Xray, fig.cap=figures("Xray1"), echo=FALSE}
plot(cars)
```

```{r Xray2, fig.cap=figures("Xray2"), echo=FALSE}
plot(cars)
```

```{r Xray3, fig.cap=figures("Xray3"), echo=FALSE}
plot(cars)
```

Some text etc. followed by `r tables("tab-DipUVvis", display="cite")`:

```{r DipUVvis, echo=FALSE}
df<-data.frame(Entry=c('AMM 51^a$','AMM 52^*a*^'),
               Precat=c('[FeBr~2~(dpbz)~2~] (4.00)','[FeBr~2~(dpbz)~2~] 
               (2.00)'))

knitr::kable(head(df), caption=tables("tab-DipUVvis", "Table Caption"))
```

2。将 Image CaptionTable Caption 设置为粗体。

在第一个 .docx 文件中,

  1. Select 图片说明或 table 说明;
  2. 加粗(Ctrl + BCommand + B);
  3. 单击主页 选项卡中样式 设置的右下角。或者按 Alt + Ctrl + Shift + S;
  4. 查找Image CaptionTable Caption
  5. 单击其 drop-down 菜单,然后单击 更新标题以匹配选择

如果您在图片和 Table 标题中都完成了上述步骤,请务必将 .docx 文件另存为工作目录中的 word-styles-reference-01.docx

3。编织 .Rmd 文件,添加 reference_docx 行以获得最终的 .docx 输出。

word_document: 行下添加 reference_docx: word-styles-reference-01.docx。请参阅以下示例中的第 7 行。

---
title: Supporting Information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
  word_document:
    reference_docx: word-styles-reference-01.docx
    fig_caption: yes
---

```{r, include=F}
library(captioner)
#`captioner` package (Ver. 2.2.3) in my envionment returns the following error messages:
#Error in captioner(prefix = "Table S", suffix = ". ", style = "b", style_prefix = TRUE,  : 
#  unused arguments (suffix = ". ", style = "b", style_prefix = TRUE)

#Error in captioner(prefix = "Figure S", suffix = ". ", style = "b", style_prefix =     TRUE,  : 
#  unused arguments (suffix = ". ", style = "b", style_prefix = TRUE)

#tables <- captioner(prefix = "Table S", suffix = ". ", style="b", style_prefix=TRUE, auto_space = FALSE)
#figures <- captioner(prefix = "Figure S", suffix = ". ", style="b", style_prefix=TRUE, auto_space = FALSE)

tables <- captioner(prefix = "Table S",  auto_space = FALSE)
figures <- captioner(prefix = "Figure S", auto_space = FALSE)

figures("Xray1", "Single-crystal X-ray structure of some text (1)", display=FALSE)
figures("Xray2", "Single-crystal X-ray structure of some text (2)", display=FALSE)
figures("Xray3", "Single-crystal X-ray structure of some text (3)", display=FALSE)
```

## Reaction of etc.
Some text. Some text followed by `r figures("Xray1", display="cite")`, which is the same figure as  `r figures("Xray3", display="cite")` but comes after `r figures("Xray2", display="cite")`.

```{r Xray, fig.cap=figures("Xray1"), echo=FALSE}
plot(cars)
```

```{r Xray2, fig.cap=figures("Xray2"), echo=FALSE}
plot(cars)
```

```{r Xray3, fig.cap=figures("Xray3"), echo=FALSE}
plot(cars)
```

Some text etc. followed by `r tables("tab-DipUVvis", display="cite")`:

```{r DipUVvis, echo=FALSE}
df<-data.frame(Entry=c('AMM 51^a$','AMM 52^*a*^'),
               Precat=c('[FeBr~2~(dpbz)~2~] (4.00)','[FeBr~2~(dpbz)~2~] 
               (2.00)'))

knitr::kable(head(df), caption=tables("tab-DipUVvis", "Table Caption"))
```

虽然没有通过 bookdown 设置图形标题前缀样式的方法,但可以通过应用自定义 Lua 过滤器(需要 pandoc 2.0 或更高版本)来实现.

Lua-Filters

假设您从

开始

Figure 1 Text of figure caption.

以下过滤器应该可以满足您的需求(请参阅 https://pandoc.org/lua-filters.html#inline 了解更多格式选项):

function Image (img)
  img.caption[1] = pandoc.Strong(img.caption[1])
  img.caption[3] = pandoc.Strong(pandoc.Str(string.gsub(img.caption[3].text, ":", ".")))
  return img
end

(假设img.caption[2]Figure之间的白色space)

再举一个例子,如果你从

开始

Figure 1 Text of figure caption.

以下过滤器应该可以满足您的需求(请参阅 https://pandoc.org/lua-filters.html#inline 了解更多格式选项):

function Image (img)
  img.caption[1] = pandoc.Strong(img.caption[1])
  img.caption[3] = pandoc.Strong(img.caption[3])
  img.caption[4] = pandoc.Strong(".  ")
  return img
end

(假设img.caption[2]Figure和数字之间的白色space,img.caption[4]是数字和标题之间的白色space )

应用 Lua-filter

假设您将此过滤器放置在 rmarkdown 文档目录中名为 figure_caption_patch.lua 的文件中,您可以通过在 YAML 前端添加 pandoc 参数来应用它:

output:
  bookdown::word_document2:
    pandoc_args: ["--lua-filter", "figure_caption_patch.lua"]

这应该会产生所需的字幕样式。

Figure 1. Text of figure caption.

有史以来最简单的解决方案,最初作为回答发布 来自 @canovasjm:

要在整个文档中将 Figure X 描述更改为 Figure SX,并将其导出为 pdf 文件,只需在 YAML 部分的 header-includes: 下使用 LaTex 命令:

---
title: "Untitled"
output: pdf_document
header-includes:
  - \renewcommand{\figurename}{Figure S}
  - \makeatletter
  - \def\fnum@figure{\figurename\thefigure}
  - \makeatother
---

```{r, fig.cap="This is my figure description", fig.height=3}
plot(pressure)
```

```{r, fig.cap="Another figure description", fig.height=3}
plot(iris$Sepal.Length, iris$Petal.Width)
```

问题发布已经有一段时间了,但是要将数字编号从 'Figure 1' 更改为 'Figure S1',您可以使用以下内容:

- \usepackage{caption}                            
- \DeclareCaptionLabelFormat{Sformat}{#1 S#2}     
- \captionsetup[table]{labelformat=Sformat}  
- \captionsetup[figure]{labelformat=Sformat}