1

I would like to show a nice cross-reference to a picture in the HTML document I'm creating with R Markdown. However, even if I followed the bookdown section on cross-references, I cannot get the reference to show in the final HTML output. I'm working in R Studio, if that helps. .Rmd file:

---
title: "ppp"
author: "ppp"
date: "July 4, 2017"
output: 
  html_document: 
    fig_caption: yes
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE)
```


```{r foo, fig.cap="$f_{p}$ as a function of $g$ for various values of $r=\\frac{\\rho_{w}}{\\rho_{a}}$"}
# All defaults
include_graphics("download.jpg")
```

A cross-reference to figure \@ref(fig:foo).

The output I get is

enter image description here

So the caption is rendering correctly, but the cross-reference is not being created. How do I fix that?

DeltaIV
  • 4,773
  • 12
  • 39
  • 86

1 Answers1

1

I'm not sure. But are you using bookdown? If you follow https://bookdown.org/yihui/bookdown/get-started.html, and use bookdown project as in https://github.com/yihui/bookdown-minimal. Then you should get the result you want.

For example, I use https://github.com/yihui/bookdown-minimal and modify the index.Rmd to something like this, and the cross reference shows correctly.

---
title: "A Book"
author: "Frida Gomam"
site: bookdown::bookdown_site
documentclass: book
output:
  bookdown::gitbook: default
  bookdown::pdf_book: default
---


```{r foo, fig.cap="$f_{p}$ as a function of $g$ for various values of $r=\\frac{\\rho_{w}}{\\rho_{a}}$", echo=FALSE}
# All defaults
knitr::include_graphics("download.png")
```

A cross-reference to figure \@ref(fig:foo).

Update: Modify the output field to bookdown::html_document2 seems to generate the html document similar to rmarkdown::html_document.

shafee
  • 15,566
  • 3
  • 19
  • 47
Consistency
  • 2,884
  • 15
  • 23
  • I hadn't understood that `bookdown` was a different package from `rmarkdown`: thanks for the clarification. Isn't it possible to get cross-references in plain `rmarkdown`? If not, I could accept your answer, **if** you modify it so that the file renders to an HTML file, instead than to an HTML file. – DeltaIV Jul 10 '17 at 21:36
  • 1
    `bookdown::html_document2` seems to be similar to `rmarkdown::html_document`. – Consistency Jul 10 '17 at 22:00