【问题标题】:R Markdown : \@ref() not workingR Markdown:\@ref() 不工作
【发布时间】:2023-03-09 15:00:01
【问题描述】:

我在获取在 R Markdown 中工作的基本参考资料时遇到了很多麻烦。为了降低我原始项目的复杂性,我决定使用 bookdown 示例代码,但我遇到了同样的问题。这是介绍示例代码的链接:https://github.com/rstudio/bookdown-demo/blob/master/01-intro.Rmd

当我将 Knitr 用于 HTML 或 PDF 时,文件生成正常,但引用不起作用,而文件将只包含“@ref(example)”。这是一张更好地显示输出的图像(我的重点以红色添加):

图片直接链接:https://i.imgur.com/2yxB5h3.png

这是一个最小的例子:

---
title: "Minimal"
output: 
  pdf_document:
    fig_caption: yes
---

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

Here is a reference to the plot below \@ref(fig:minGraph)

```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```

输出如下所示: https://i.imgur.com/J3UECqn.png

【问题讨论】:

  • 关于这个 GitHub 问题的最后一条评论看起来可能有用:github.com/rstudio/bookdown/issues/62
  • 嗨@CristianE.Nuno,感谢您的链接。数字的实际标题工作正常,就在我尝试引用常规文本正文中的任何内容时。
  • 你能提供一个minimal reproducible example吗?从屏幕截图来看,您使用的是rmarkdown::html_document 进行渲染,而不是bookdown 方法之一。
  • 嗨@RalfStubner,绝对。另外,你是完全正确的,我误以为这是 bookdown,我只是使用 rmarkdown。我在原始帖子中添加了一个示例。

标签: r r-markdown


【解决方案1】:

如果您想在普通的rmarkdown 文档中使用bookdown 扩展,您可以使用bookdown::html_document2bookdown::pdf_document2 而不是rmarkdown::html_documentrmarkdown::pdf_document。示例:

---
title: "Minimal"
output: 
  bookdown::html_document2:
    fig_caption: yes
  bookdown::pdf_document2:
    fig_caption: yes
---

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

Here is a reference to the plot below \@ref(fig:minGraph)

```{r minGraph, echo=FALSE, fig.cap="test"}
plot(x=1)
```

【讨论】:

  • 您能否提供对 knitr 或 bookdown::pdf_document2 的调用?我正在努力让它在 R 会话中从控制台工作。谢谢!
  • @AndyClifton 在 R 控制台中,您必须使用 rmarkdown::render()
  • Ralph - 明白,但您能完整接听电话吗?我在运行 bookdown::pdfdocumet2 时遇到问题,并且正在运行其他 MWE 以试图解决这个问题。谢谢!
  • @Andy, rmarkdown::render("path/to/file.Rmd") 将使用第一个定义的格式。您还可以使用rmarkdown::render("path/to/file.Rmd", output_format = "all") 使用文件中给定的所有格式,或使用rmarkdown::render("path/to/file.Rmd", output_format = c("bookdown::pdf_document2", "bookdown::html_document2")) 指定输出格式列表。有关详细信息,请参阅?rmarkdown::render
  • 给自己和其他人的注意事项:块名称中的下划线不适用于此类引用
【解决方案2】:

看起来我在仅使用 R markdown 时阅读了 bookdown 指南,使我的语法感到困惑。感谢 Ralf 为我指明了这个方向。正确的最小代码应该是这样的:

---
title: "Minimal"
output: 
  pdf_document:
    fig_caption: yes
---

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

Here is a reference to the plot below \ref{fig:minGraph}

```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2018-10-24
    • 2021-08-22
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多