如何在书本中更改图形标题格式

时间:2018-08-21 08:56:47

标签: r rstudio r-markdown bookdown

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

  

图1图形标题的文字。

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

  

图1。图标题的文本。

以及有关支持信息文档:

  

图S1。图标题文字。

在图中的文字参考中,我们还需要:

  

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

因此参考文字不应为粗体。

问题

如何使书本(或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。(请注意,其全部为粗体,最后是句号)。

4 个答案:

答案 0 :(得分:4)

据我所知,您无法通过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"))
```

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

答案 1 :(得分:3)

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

  

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

但是,制作这样的图片似乎仍然很困难:

  

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

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

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$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. 点击其下拉菜单,然后点击更新标题以匹配选择

如果您已完成“图像标题”和“表标题”中的上述步骤,请确保将.docx文件另存为工作目录中的word-styles-reference-01.docx

Selecting an image caption to make it bold

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

答案 2 :(得分:2)

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

假设您是从

开始的
  

图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[3]Figure和数字之间的空白,而img.caption[4]是数字和标题之间的空白)

假设将此过滤器放置在figure_caption_patch.lua文档目录中名为rmarkdown的文件中,则可以通过在YAML前题中添加pandoc自变量来应用该过滤器:

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

这将产生所需的字幕样式。

  

图1。图形标题文字。

答案 3 :(得分:0)

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

要将整个文档中的 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)
```

enter image description here