cross-reference to figure in R Markdown with html output is not working

落花浮王杯 提交于 2019-12-24 11:09:01

问题


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.



来源:https://stackoverflow.com/questions/45018180/cross-reference-to-figure-in-r-markdown-with-html-output-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!