簡體   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