0

Can anyone help me understand how to write my header so that the figure caption and cross reference works?

I am practicing making captions and cross references to a simple plot in my Rmd file. I understand that to do so, I should add to my header: "output: bookend::pdf_document2" and "fig_caption = yes". Then, to a chunk called myfigure, I should add "fig.cap = "\label{fig:myfigure} My caption". To cross reference this figure I should write in the text "@ref(fig:myfigure)". My code is below. It won't knit because the formatting of the header is wrong.

---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
  toc: true
  fig_caption: yes
---

```{r myfigure, fig.cap = "\\label{fig:myfigure} My caption"}
plot(pressure)
```

My plot is called \@ref(fig:myfigure).

Then, I tried deleting the whitespace before toc and fig_caption, and it knit, but no caption appeared, and the text literally printed "@ref(fig:myfigure)" instead of a cross reference. The header I tried is here:

---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---

I also tried adding "pdf_document:" to the header, but the same issue of no caption and the cross reference being literally "@ref(fig:myfigure)". This header I tried is here:

 ---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
  pdf_document:
    toc: true
    fig_caption: yes
---

Can anyone help me understand how to write my header so that it works?

0

1 Answer 1

1

The YAML header of your document is incorrect and produces an error when I try to run it. Using

---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: 
  bookdown::pdf_document2:
    toc: true
    fig_caption: yes
---

```{r myfigure, fig.cap = "\\label{fig:myfigure} My caption"}
plot(pressure)
```

My plot is called \@ref(fig:myfigure).

I get the expected result:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .