首页 > 解决方案 > Rmarkdown/Bookdown:补充部分的单独数字编号

问题描述

某些类型的文件,例如期刊文章,通常有一个补充部分,其中的数字编号与正文不同。

例如,在主体中,您可能有图 1-5。但是,对于补充部分,编号重新开始,如图 S1、S2、S3 等。

Bookdown 允许交叉引用(\@ref(fig:label)但我不确定如何在单独的部分重新开始编号。有没有好的方法呢?

标签: latexr-markdownbookdown

解决方案


您可以在文件的 YAML 标头中定义一个新的LaTeX 函数.rmd,如下所示:

\newcommand{\beginsupplement}{
  \setcounter{table}{0}  
  \renewcommand{\thetable}{S\arabic{table}} 
  \setcounter{figure}{0} 
  \renewcommand{\thefigure}{S\arabic{figure}}
}

然后\beginsupplement在您准备好开始使用 S1、S2... 等标记图形和表格时键入。如果您仅导出为 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")
```

在此处输入图像描述


推荐阅读