Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross reference and caption not working in Rmd file

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?

like image 663
chase171 Avatar asked Sep 17 '25 01:09

chase171


1 Answers

use \ref{fig:myfigure} instead of \\@ref(fig:myfigure)

See RStudio Community Post

like image 92
Jhou Avatar answered Sep 19 '25 16:09

Jhou