R Markdown HTML Number Figures

前端 未结 4 756
[愿得一人]
[愿得一人] 2020-12-01 08:40

Does anyone know how to number the figures in the captions, for HTML format R Markdown script?

For PDF documents, the caption will say something like:

4条回答
  •  感情败类
    2020-12-01 08:59

    So unless someone has a better solution, this is the solution that I came up with, there are some flaws with this approach (for example, if the figure/table number is dependent on the section number etc...), but for the basic html document, it works.

    Somewhere at the top of you document, run this:

    ```{r echo=FALSE}
    #Determine the output format of the document
    outputFormat   = opts_knit$get("rmarkdown.pandoc.to")
    
    #Figure and Table Caption Numbering, for HTML do it manually
    capTabNo = 1; capFigNo = 1;
    
    #Function to add the Table Number
    capTab = function(x){
      if(outputFormat == 'html'){
        x = paste0("Table ",capTabNo,". ",x)
        capTabNo <<- capTabNo + 1
      }; x
    }
    
    #Function to add the Figure Number
    capFig = function(x){
      if(outputFormat == 'html'){
        x = paste0("Figure ",capFigNo,". ",x)
        capFigNo <<- capFigNo + 1
      }; x
    }
    ```
    

    Then during the course of your document, if say you want to plot a figure:

    ```{r figA,fig.cap=capFig("My Figure Caption")
    base = ggplot(data=data.frame(x=0,y=0),aes(x,y)) + geom_point()
    base
    ```
    

    Substitute the capFig to capTab in the above, if you want a table caption.

提交回复
热议问题