简体   繁体   English

在 rmarkdown 中为图形/绘图添加标题

[英]Adding captions to figures/plots in rmarkdown

I'm using rmarkdown to output a PDF document, however adding captions to plots seems not to be working.我正在使用 rmarkdown 输出 PDF 文档,但是向绘图添加标题似乎不起作用。 According to the docs, we're supposed to be using fig.cap to specify figure captions to pass to Latex.根据文档,我们应该使用fig.cap来指定要传递给 Latex 的图形标题。 This is the header of my code chunk:这是我的代码块的标题:

```{r Plot bond index returns, include = TRUE, fig.cap = "Bond index cumulative returns"}

I made sure to include the following lines in the header of the rmarkdown document我确保在 rmarkdown 文档的标题中包含以下几行

output: pdf_document: fig_caption: true

This setup eliminates the entire chunk's output from the resulting PDF completely此设置从生成的 PDF 中完全消除了整个块的输出

Can you provide a little more detail?你能提供更多细节吗? I can't reproduce this error.我无法重现此错误。 With a barebones .Rmd that looks like:使用准系统 .Rmd 看起来像:

---
output: pdf_document
---

```{r echo = FALSE, fig.cap = "Test figure caption."}
plot(pressure)
```

I get an output like:我得到如下输出:

在此处输入图片说明

Edit:编辑:

After looking at the answers to this question , the following code generates figures with text interspersed with the figures:查看此问题的答案后,以下代码生成带有文本穿插数字的数字:

---
output: pdf_document
header-includes:
 \usepackage{float}
 \floatplacement{figure}{H}
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'h')
```

Here is text number preceding figure 1

```{r echo = FALSE, fig.cap = "Test figure caption."}
plot(pressure)
```

Here is text following figure 1

```{r echo = FALSE, fig.cap = "Second test figure caption."}
plot(cars)
```

Here is some final text following the second figure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM