cross-reference to figure in R Markdown with html

2019-07-25 05:41发布

问题:

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

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

回答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.