Search code examples
rlatexr-markdownfigure

errors in figure caption in r markdown caption outside float


I tried to put the plot objects in r markdown. The fig.cap worked well in the first r chunk. However problems occurred when I tried to put the second figure. Please see the code below.

The YAML looks like this

output:
  bookdown::pdf_document2:
    toc: no
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{amsmath}
- \usepackage{multicol}
- \usepackage{threeparttable}
- \usepackage{caption}

The code in r chunk is

{r trend_1, echo=F, fig.cap='The air pollution trend of different groups',out.width='50%',message=F, fig.show='hold'}

The code for plotting is (these are two plot objects)

state.con.oz
state.con.pm

I got an error ! Package caption Error: \caption outside float. Any ideas?

BTW, I wonder how to create a figure footnote in r markdown?

Thanks in advance.


Solution

  • Try to name your R code chunk without underlines

    ```{r trend1, echo=F, fig.cap='The air pollution trend of different groups',out.width='50%',message=F, fig.show='hold'}
    
    your_plot
    ```