How to make a figure caption in Rmarkdown?

前端 未结 3 820
余生分开走
余生分开走 2021-02-02 17:28

I am thinking about writing my thesis with rmarkdown and latex. I\'m getting the hang of how it all works, however, when I try to add a figure (not an R plot) to the text and re

3条回答
  •  余生分开走
    2021-02-02 17:53

    I just found a very useful solution here.

    First, include the following chunk:

    ```{r functions, include=FALSE}
    # A function for captioning and referencing images
    fig <- local({
        i <- 0
        ref <- list()
        list(
            cap=function(refName, text) {
                i <<- i + 1
                ref[[refName]] <<- i
                paste("Figure ", i, ": ", text, sep="")
            },
            ref=function(refName) {
                ref[[refName]]
            })
    })
    ``` 
    

    After, we can add the caption of the figure/table in the figure chunk options like:

    ```{r, fig.cap=paste("Your caption.")}
    
    • See that fig.cap works better with paste.

提交回复
热议问题