【问题标题】:Figures captions and labels in knitrknitr 中的图形标题和标签
【发布时间】:2015-11-02 18:49:45
【问题描述】:

我是 Knitr 的新手。我正在尝试使用 r 块制作报告,但我不知道如何使用标题和标签来稍后引用该图。 这是我想做的一个例子:

---
title: "Plotting"

author: "xx"

date: '2015-08-10'

output: pdf_document
---
```{r figs, echo=FALSE, fig.width=7,fig.height=6,fig.cap="plotting example"}

    par(mfrow=c(2,2))
    plot(1:10, col=2)
    plot(density(runif(100, 0.0, 1.0)))
    plot(runif(100, 0.0, 1.0),type="l")
```

in Figure \ref{fig:figs} we see examples of plotting in R.

我想要一个标题“Plotting example”,并有一个标签,所以我可以在文本中使用 Figure \ref{fig.label}。我已经尝试过 fig.cap 和 fig.lp,它们都不起作用。 如果有人可以提供帮助,我将不胜感激。

【问题讨论】:

  • 欢迎来到 SO。下次您发布问题时,如果可能(例如这种情况)尝试包含reproducible example,无论如何fig.cap 块选项适合您。请参阅here RMarkdown 的完整指南。您的问题是反向引用的问题,但如果输出是 PDF 或 HTML,解决方案可能会有所不同。这就是为什么一个可重复的例子很重要的原因。

标签: r knitr r-markdown


【解决方案1】:

您可以通过在标题中包含fig_caption: yes 来实现此目的:

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

```{r figs, echo=FALSE, fig.width=7,fig.height=6,fig.cap="\\label{fig:figs}plotting example"}
par(mfrow=c(2,2))
plot(1:10, col=2)
plot(density(runif(100, 0.0, 1.0)))
plot(runif(100, 0.0, 1.0),type="l")
```

in Figure \ref{fig:figs} we see examples of plotting in R.

请注意,图片标题标签应包含在带有双反斜杠的标题中,如上所示。

【讨论】:

  • 这对我不起作用,可能是因为我使用的是 VS 2017 而不是 RStudio。如果您遇到与我相同的问题,请点击此链接获取an elegant manual solution
  • 为我工作而没有说明fig_caption: yes
  • 使用html_document,“图1:”部分不会写入html文件。
  • 我想知道Figure 1: 部分怎么写,就像@user2955884 一样
  • 如果我想为每个情节添加副标题,如“a”、“b”、“c”?
猜你喜欢
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 2021-08-30
  • 1970-01-01
相关资源
最近更新 更多