如何在bookdown中更改数字标题格式

AMM*_*AMM 13 r rstudio r-markdown bookdown

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

图1图标题文字.

在化学中,惯例是将主要数字标记为:

图1.图标题文字.

并为支持信息文件:

图S1.图标题文字.

另外在我们需要的文字参考文献中:

......如图1所示,......

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

题

如何使用bookdown(或rmarkdown)生成图形和表格标题,如下所示:" 图S1.一些文字." 和" 表S1.一些文字." ?

我需要这是MS Word格式.

示例/尝试的解决方案

到目前为止,我试图修改_bookdown.yml文件如下:

language:
  label:
    fig: "**Figure S**"
    tab: "**Table S**"
Run Code Online (Sandbox Code Playgroud)

这给出了:图S 1一些文本......以及使用时的内联引用:

Figure S\@ref(fig:Xray)
Run Code Online (Sandbox Code Playgroud)

是图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$3^a$','AMM 52^*a*^'),
               Precat=c('[FeBr~2~(dpbz)~2~] (4.00)','[FeBr~2~(dpbz)~2~] 
(2.00)'))

kable(head(df), format = "markdown")
```
Run Code Online (Sandbox Code Playgroud)

上面的代码产生图小号 1图中的标题但不是图S1.(注意它全部都是粗体,最后是句号).

LmW*_*mW. 8

据我所知,您无法控制图形/表格标题以使用 rmarkdown/bookdown 执行您想要的操作。您可以使用该包captioner来实现它,但有一个问题:它适用于rmarkdown输出,但您需要对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$3^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"))
```
Run Code Online (Sandbox Code Playgroud)

但是,如果您切换到使用 bookdown::word_document2 输出,则图标题变为“图 1:图 S1. ...”。我还没有找到抑制“图 1:”的方法,并且必须在我的输出中进行后处理以搜索所有“图?:”并将其替换为“”。

  • @AMM,我认为您用`captioner` 描述的“问题”是因为它在第一次将唯一值传递给“name”参数时分配图形/表格编号,无论“display”参数的值是多少是。更好的解决方案是使用“display=FALSE”以您想要的顺序定义您的数字,然后在您想要的任何地方引用或显示它们。我修改了我的示例代码以包含这样一个示例。 (2认同)

crs*_*rsh 8

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

Lua 过滤器

假设你从

图 1 图标题文字。

以下过滤器应该可以满足您的需求(有关其他格式选项,请参阅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
Run Code Online (Sandbox Code Playgroud)

(假设这img.caption[2]是之间的空白Figure)

再举一个例子,如果你从

图 1 图标题文字。

以下过滤器应该可以满足您的需求(有关其他格式选项,请参阅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
Run Code Online (Sandbox Code Playgroud)

(假设这img.caption[2]是Figure数字和img.caption[4]数字之间的空格,是数字和标题之间的空格)

应用 Lua 过滤器

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

output:
  bookdown::word_document2:
    pandoc_args: ["--lua-filter", "figure_caption_patch.lua"]
Run Code Online (Sandbox Code Playgroud)

这应该会产生所需的标题样式。

图 1.图标题文字。

  • 看起来 rmarkdown 发生了变化,默认情况下会添加一个冒号,因此您的解决方案不再起作用。请参阅 https://community.rstudio.com/t/how-to-change-the-figure-table-caption-style-in-bookdown/110397。但是你可以做`img.caption[3] = pandoc.Strong(pandoc.Str(string.gsub(img.caption[3].text, ":", ".")))`来替换`:` . 通过`.`。请参阅 /sf/ask/4795392591/#68508668。 (2认同)

Car*_*era 6

按照本指南设置 Word ( .docx) 样式,您可以制作无花果。和选项卡。标题粗体,虽然整个标题行可以是粗体...我的意思是我们有一种方法可以.docx通过 RMarkdown 自动创建一个标题:

图S1:部分文字的单晶X射线结构(1)

然而,制作这样一个似乎仍然很困难:

图 S1 : 一些文字的单晶 X 射线结构 (1)

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

1.针织的.Rmd文件@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$3^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"))
```
Run Code Online (Sandbox Code Playgroud)

2. 设置Image Caption和Table Caption为粗体。

在第一个.docx文件中,

  1. 选择图像标题或表格标题;
  2. 加粗(Ctrl+B或Command+ B);
  3. 单击主页选项卡中样式设置的右下角。或按+ + + ;AltCtrlShiftS
  4. 查找Image Caption或Table Caption;
  5. 单击其下拉菜单并单击Update Title to match selection。

如果您在 Image 和 Table Caption 中都完成了上述步骤,请确保将.docx文件保存word-styles-reference-01.docx在您的工作目录中。

选择图像标题以使其加粗

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

添加reference_docx: word-styles-reference-01.docx以下word_document:行。请参阅以下示例中的第 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$3^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"))
```
Run Code Online (Sandbox Code Playgroud)