如何更改 bookdown 中的图形标题格式 [英] How to change the figure caption format in bookdown

查看:61
本文介绍了如何更改 bookdown 中的图形标题格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When using bookdown (single document), figures are numbered automatically as:

图 1 图标题文字.

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

In chemistry, the convention is to label main Figures as:

图 1. 图标题文字.

以及支持信息文件:

图 S1. 图标题文字.

也在文中的图参考中我们需要:

Also in the figure reference in the text we need:

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

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

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

so the reference text should not be bold.

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

How can i make bookdown (or rmarkdown) produce figure and table captions like so: "Figure S1. Some text." and "Table S1. Some text." ?

我需要将其设为 MS Word 格式.

I need this to be in MS Word format.

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

So far i tried to modify the _bookdown.yml document as follows:

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

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

This gives: Figure S1 Some text... and the inline reference when using:

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$3^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.(注意它全部是粗体,最后是句号).>

The code above produces Figure S1 in the figure caption but NOT Figure S1. (Note it is all bold and a full stop in the end).

推荐答案

据我所知,您无法使用 rmarkdown/bookdown 控制图形/表格标题来做您想做的事情.您可以使用包 captioner 来实现它,有一个问题:它适用于 rmarkdown 输出,但您需要使用 bookdown 进行后期处理 输出.这是生成所需字幕样式的 Rmd 文件:

To my knowledge, you cannot control figure/table captions to do what you want with rmarkdown/bookdown. You can use the package captioner to achieve it, with a catch: it works fine with rmarkdown outputs, but you'll need to do post-processing with bookdown outputs. Here is the Rmd file that produces your desired caption style:

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

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

However, if you switch to use bookdown::word_document2 output, the figure caption becomes "Figure 1: Figure S1. ...". I haven't found a way to suppress "Figure 1:" and have to do post-processing in my output to search and replace all "Figure ?:" with "".

这篇关于如何更改 bookdown 中的图形标题格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆