knitr将参数fig.cap定义为
fig.cap :( NULL;字符)图中使用的图标题 LaTeX 中的环境(在\ caption {}中);如果为NULL或NA,则为 忽略,否则数字环境将用于图中 块(在\ begin {figure}和\ end {figure}中输出)
但是,对于HTML输出,以下工作:
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
```
```{r, fig.cap = c("This is caption 1", "This is caption 2")}
## Plot 1
qplot(carat, price, data = diamonds)
## Plot 2
qplot(carat, depth, data = diamonds)
```
意思是,每个数字都会在代码块参数fig.cap = c("Caption 1", "Caption 2")
但是,跟踪字幕是很困难的 - 特别是如果长时间将它们放在块选项中。除了为每个图形创建两个单独的块,并且在块外部插入了标题,还有其他选项吗?
答案 0 :(得分:9)
您可以设置eval.after="fig.cap"
,以便在块运行后评估图标题。这样,您就可以在块中定义标题。
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
library(knitr)
opts_knit$set(eval.after = 'fig.cap')
```
```{r, fig.cap = cap}
## Plot 1
qplot(carat, price, data = diamonds)
cap <- "This is caption 1"
## Plot 2
qplot(carat, depth, data = diamonds)
cap <- c(cap, "This is caption 2")
```