1
votes

So. I want to insert some image files into an Rmarkdown document, auto-generate labels and be able to reference those images from elsewhere in the text. I'm using bookdown, so I start off with

---
output: 
  html_document:
    toc: true
    number_sections: true
    fig_caption: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(bookdown)
```

According to the bookdown manual if I have the following code chunk:

```{r knnPlot, echo=FALSE, fig.cap="Knn Plot"}
    knitr::include_graphics("knn-cs3-gs2.png")
```

then I should have a label fig:knnPlot automatically generated, because using fig.cap apparently puts R in a figure environment and automatically prepend "fig" to the label. It then tells me I should be able to reference the figure using \@ref(label), or rather, in this case, \@ref(fig:knnPlot)

When I do this however, the text "\@ref(fig:knnPlot)" is output, rather than an actual cross reference. There's no figure label, no numbering. How is this meant to work?

I can't get the hard coding method suggested here to work. Nor can I get the only other option I can find to work, it tells me to install pandoc-crossreference, which leads me down an absurd rabbit hole of installing haskell of all things along with endless dependencies and obscure pointless error messages which lead to spectacularly unhelpful developer forums filled with 6 pages of error logs.

1

1 Answers

2
votes

You are not creating a bookdown document. Use bookdown::html_document2 instead:

---
title: "Bookdown"
output: 
  bookdown::html_document2:
    fig_caption: yes
    number_sections: yes
    toc: yes
---

```{r Doge, echo=FALSE, fig.cap="Mighty Doge"}
knitr::include_graphics("unnamed.png")
```

Check out this picture: \@ref(fig:Doge)

enter image description here