繁体   English   中英

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

[英]How to change the figure caption format in bookdown

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

图 1 图标题文字。

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

图 1.图标题文字。

以及支持信息文件:

图 S1。 图标题的文本。

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

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

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

我怎样才能让 bookdown(或 rmarkdown)产生像这样的图和表标题:“图 S1。一些文本。” 和“表 S1。一些文字。” ?

我需要这个是MS Word格式。

示例/尝试的解决方案

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

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

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

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

上面的代码在图标题中生成图 S 1,但不是图 S1。 (请注意,所有内容都是粗体,最后是句号)。

虽然没有通过bookdown图形标题前缀样式的方法,但可以通过应用自定义 Lua 过滤器(需要pandoc 2.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

(假设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

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

应用 Lua 过滤器

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

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

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

图 1.图标题文字。

据我所知,您无法控制图形/表格标题以使用 rmarkdown/bookdown 执行您想要的操作。 您可以使用 package 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"))
```

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

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

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

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

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

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

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

2. 设置Image CaptionTable Caption为粗体。

在第一个.docx文件中,

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

如果您在图像和表格标题中都完成了上述步骤,请确保将.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$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"))
```

最简单的解决方案不断,最初发布的答案在这里从@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)
```

在此处输入图片说明

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

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

暂无
暂无

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

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