Search code examples
rr-markdownbookdown

Questions about referencing figures in R Markdown (PDF output)?


I am trying to knitting out a R markdown as pdf document. I want to autoreference my figures so that when I mention figure 1 in my text, it can automatically direct to the figure 1 in the figure section.

I downloaded bookdown package and tried the following code as I used the introduction to bookdown guide. However, after I knitted it, it showed up what I typed in, instead of the label I wanted it to show.

Here is my YAML header:

---
title: Policy Analysis
author: Me
fontsize: 12pt
output:
  pdf_document: default
  bookdown::pdf_document2: default
mainfont: Times New Roman
geometry: margin=2.54cm
subtitle: Web address
abstract: "This is an analysis"
  ---

And here below is my code:

library('bookdown')

\@ref(fig:explore) is produced to look at each of the 9 counties and their respective air emission.

```{r explore, fig.cap="Data Exploration", fig.height = 5.5, fig.width = 9}

par(mar = c(4, 4, .1, .1))

plot(cars, pch = 19)


```

I expect it to be directing me to the graph that is generated by the code chuck explore, but the text ended up still appearing as what I typed it is:

@ref(fig:explore) is produced to look at each of the 9 counties and their respective air emission.

Basically, @ref(fig:explore) didn't do anything to reference the figure.

And the graph is shown below with its caption.

Is there anything wrong with my setting? Please let me know if anyone has any thoughts on this.

Thank you!


Solution

  • You have not provided your YAML header, but I think there is the problem. It should have the following line:

    --- 
    output: 
      bookdown::pdf_document2
    ---
    

    You can not use the features of bookdown without loading the templates from the package.