Rmarkdown/Bookdown:补充部分的单独图编号
Rmarkdown/Bookdown: Separate figure numbering for Supplemental Section
某些类型的文档,例如期刊文章,通常有补充部分,其中图的编号与正文不同。
例如,在正文中,您可能有图 1-5。但是,对于补充部分,编号重新开始为图 S1、S2、S3 等。
Bookdown 允许交叉引用(\@ref(fig:label)
但我不确定如何在单独的部分中重新开始编号。有什么好的方法吗?
您可以在 .rmd
文件的 YAML header 中定义一个新的 LaTeX function,如下所示:
\newcommand{\beginsupplement}{
\setcounter{table}{0}
\renewcommand{\thetable}{S\arabic{table}}
\setcounter{figure}{0}
\renewcommand{\thefigure}{S\arabic{figure}}
}
然后在您准备好开始使用 S1、S2 等标记图形和表格时键入 \beginsupplement
。如果您仅导出为 PDF,此解决方案工作正常,因为它使用 LaTeX 命令来格式化输出。因此,它不适用于 HTML 或 Word 输出。
---
title: "title"
author:
- My Namington*
- '*\textit{email@example.com} \vspace{5mm}'
output:
bookdown::pdf_document2
fontsize: 12pt
header-includes:
\usepackage{float} \floatplacement{figure}{H}
\newcommand{\beginsupplement}{\setcounter{table}{0} \renewcommand{\thetable}{S\arabic{table}} \setcounter{figure}{0} \renewcommand{\thefigure}{S\arabic{figure}}}
---
```{r, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
```
# Main text
Here is the main text of my paper, and a link to a normally-labelled Figure \@ref(fig:irisPlot).
```{r irisPlot, fig.cap="This is a figure caption."}
ggplot(iris, aes(Species, Sepal.Length, colour = Species)) + geom_jitter()
```
\newpage
# Supplementary material {-}
\beginsupplement
Here is the supplement, including a link to a figure prefixed with the letter S Figure \@ref(fig:irisPlot2).
```{r irisPlot2, echo=FALSE, fig.cap= "This is a supplementary figure caption."}
ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) +
geom_point() +
stat_smooth(method = "lm")
```
对于那些需要适用于 Word DOCX 的东西的人,这是一个迟来的答案,基于此 。
---
output: officedown::rdocx_document
---
```{r setup, include=FALSE}
pacman::p_load(officedown, officer, knitr)
knitr::opts_chunk$set(echo = FALSE)
## Custom function to restart numbering at the start of each new chapter.
## You could also just do this manually!
new_chapter <- function(){
if(!exists("chapter_count")) chapter_count <<- 0
chapter_count <<- chapter_count + 1
}
```
# Chapter 1: Red section
```{r fig.id="red-plot1"}
new_chapter()
barplot(1:8, col = "red4")
block_caption("Some red bars",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
start_at = 1, ##restart
bkm = 'red-plot1',
pre_label = paste0("Figure ", chapter_count, ".")))
```
Figure `r chapter_count`.\@ref(fig:red-plot1) shows some red bars.
# Chapter 2 : Blue section
```{r fig.id="blue-plot1"}
new_chapter()
barplot(1:8, col = "dodgerblue3")
block_caption("Some blue bars",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
start_at = 1, ##restart
bkm = 'blue-plot1',
pre_label = paste0("Figure ", chapter_count, ".")))
```
Figure `r chapter_count`.\@ref(fig:blue-plot1) shows some blue bars.
```{r fig.id="blue-plot2"}
barplot(8:1, col = "dodgerblue3")
block_caption("More blue bars",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
bkm = 'blue-plot2',
pre_label = paste0("Figure ", chapter_count, ".")))
```
Figure `r chapter_count`.\@ref(fig:blue-plot2) shows some more blue bars.
# Supplementary section
```{r fig.id="supp-plot1"}
barplot(1:4, main = "Supplementary bars" )
block_caption("Some supplementary bars",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
start_at = 1, ##restart count
bkm = 'supp-plot1',
pre_label = "Figure S"))
```
Figure S\@ref(fig:supp-plot1) shows some supplementary bars
某些类型的文档,例如期刊文章,通常有补充部分,其中图的编号与正文不同。
例如,在正文中,您可能有图 1-5。但是,对于补充部分,编号重新开始为图 S1、S2、S3 等。
Bookdown 允许交叉引用(\@ref(fig:label)
但我不确定如何在单独的部分中重新开始编号。有什么好的方法吗?
您可以在 .rmd
文件的 YAML header 中定义一个新的 LaTeX function,如下所示:
\newcommand{\beginsupplement}{
\setcounter{table}{0}
\renewcommand{\thetable}{S\arabic{table}}
\setcounter{figure}{0}
\renewcommand{\thefigure}{S\arabic{figure}}
}
然后在您准备好开始使用 S1、S2 等标记图形和表格时键入 \beginsupplement
。如果您仅导出为 PDF,此解决方案工作正常,因为它使用 LaTeX 命令来格式化输出。因此,它不适用于 HTML 或 Word 输出。
---
title: "title"
author:
- My Namington*
- '*\textit{email@example.com} \vspace{5mm}'
output:
bookdown::pdf_document2
fontsize: 12pt
header-includes:
\usepackage{float} \floatplacement{figure}{H}
\newcommand{\beginsupplement}{\setcounter{table}{0} \renewcommand{\thetable}{S\arabic{table}} \setcounter{figure}{0} \renewcommand{\thefigure}{S\arabic{figure}}}
---
```{r, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
```
# Main text
Here is the main text of my paper, and a link to a normally-labelled Figure \@ref(fig:irisPlot).
```{r irisPlot, fig.cap="This is a figure caption."}
ggplot(iris, aes(Species, Sepal.Length, colour = Species)) + geom_jitter()
```
\newpage
# Supplementary material {-}
\beginsupplement
Here is the supplement, including a link to a figure prefixed with the letter S Figure \@ref(fig:irisPlot2).
```{r irisPlot2, echo=FALSE, fig.cap= "This is a supplementary figure caption."}
ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) +
geom_point() +
stat_smooth(method = "lm")
```
对于那些需要适用于 Word DOCX 的东西的人,这是一个迟来的答案,基于此
---
output: officedown::rdocx_document
---
```{r setup, include=FALSE}
pacman::p_load(officedown, officer, knitr)
knitr::opts_chunk$set(echo = FALSE)
## Custom function to restart numbering at the start of each new chapter.
## You could also just do this manually!
new_chapter <- function(){
if(!exists("chapter_count")) chapter_count <<- 0
chapter_count <<- chapter_count + 1
}
```
# Chapter 1: Red section
```{r fig.id="red-plot1"}
new_chapter()
barplot(1:8, col = "red4")
block_caption("Some red bars",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
start_at = 1, ##restart
bkm = 'red-plot1',
pre_label = paste0("Figure ", chapter_count, ".")))
```
Figure `r chapter_count`.\@ref(fig:red-plot1) shows some red bars.
# Chapter 2 : Blue section
```{r fig.id="blue-plot1"}
new_chapter()
barplot(1:8, col = "dodgerblue3")
block_caption("Some blue bars",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
start_at = 1, ##restart
bkm = 'blue-plot1',
pre_label = paste0("Figure ", chapter_count, ".")))
```
Figure `r chapter_count`.\@ref(fig:blue-plot1) shows some blue bars.
```{r fig.id="blue-plot2"}
barplot(8:1, col = "dodgerblue3")
block_caption("More blue bars",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
bkm = 'blue-plot2',
pre_label = paste0("Figure ", chapter_count, ".")))
```
Figure `r chapter_count`.\@ref(fig:blue-plot2) shows some more blue bars.
# Supplementary section
```{r fig.id="supp-plot1"}
barplot(1:4, main = "Supplementary bars" )
block_caption("Some supplementary bars",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
start_at = 1, ##restart count
bkm = 'supp-plot1',
pre_label = "Figure S"))
```
Figure S\@ref(fig:supp-plot1) shows some supplementary bars