knitr 中的数字标题和标签

Figures captions and labels in knitr

我是 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",并有一个标签,所以我可以在文本中使用图 \ref{fig.label}。我试过 fig.cap 和 fig.lp,其中 none 有效。 如果有人能提供帮助,我将不胜感激。

您可以通过在 header 中包含 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.

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