【问题标题】:Rmarkdown/Bookdown: Separate figure numbering for Supplemental SectionRmarkdown/Bookdown:补充部分的单独数字编号
【发布时间】:2018-05-07 22:07:40
【问题描述】:

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

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

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

【问题讨论】:

    标签: latex r-markdown bookdown


    【解决方案1】:

    您可以在 .rmd 文件的 YAML 标头中定义一个新的 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")
    ```
    

    【讨论】:

    • 我稍微调整了答案以使用 bookdown 进行交叉引用并澄清一些要点(它并不完全保证我将其发布为我自己的答案)。 bookdown.org/yihui/bookdown/cross-references.html
    • 谢谢迈克!也许我的第一个答案会被接受:)
    【解决方案2】:

    对于那些需要适用于 Word DOCX 的东西的人,这里是一个迟来的答案,基于 Restart Figure Numbering for Appendix / Supplementary Material in 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2017-02-28
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 2022-07-27
      相关资源
      最近更新 更多