Rmarkdown/Bookdown: Separate figure numbering for Supplemental Section

后端 未结 1 2004
陌清茗
陌清茗 2021-01-12 02:30

Certain kinds of documents, such as journal articles, often have a Supplemental Section, where the numbering of figures is different from the main body.

For example,

相关标签:
1条回答
  • 2021-01-12 02:51

    You can define a new LaTeX function in the YAML header of your .rmd file as follows:

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

    Then type \beginsupplement when you're ready to start labelling the figures and tables with S1, S2... etc. This solution works fine if you export to PDF only, as it uses LaTeX commands to format the output. It therefore will not work for HTML or Word outputs.

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

    0 讨论(0)
提交回复
热议问题