Rmarkdown/Bookdown:副本章节独立编号的图表

17

某些文档(例如期刊文章)通常有附加部分,其中图表编号与主体不同。

例如,在主体中,你可能会有图1-5。但是,在附加部分中,编号重新开始,如图S1、S2、S3等。

Bookdown 允许跨引用 (\@ref(fig:label)),但我不确定如何在单独的部分重新启动编号。有没有好方法可以做到这一点?

2个回答

19

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

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

enter image description here


我稍微调整了答案,使用了bookdown的交叉引用,并澄清了一些要点(这并不足以让我将其发布为自己的答案)。https://bookdown.org/yihui/bookdown/cross-references.html - Michael Harper
谢谢Mike!也许我会得到我的第一个被接受的答案 :) - lukeholman

1

对于那些需要适用于Word DOCX的东西的人,这里是一个迟来的答案,基于这个在bookdown中为附录/补充材料重新启动图表编号

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接