在knitr中跨行分割r块标题

时间:2015-11-10 10:51:37

标签: r knitr r-markdown

当我在我的R块标题中插入长字幕等时,能够将标题分割成多行是很好的。

有没有简单的方法可以做到这一点?

E.g:

```{r, echo=FALSE, warning=FALSE, 
    fig.cap="Here is my really long caption.  It'd be nice to split this and other portions across lines"}
    print(plot(x=runif(100),y=runif(100)))
```

1 个答案:

答案 0 :(得分:10)

不,您不能在块选项中插入换行符。来自the manual

  

块选项必须写在一行;块选项中不允许换行

但是,如果你在编辑器中拼命想要整洁的格式化,你可以通过一个额外的变量绕道而行,但这会使代码膨胀很多:

---
output: 
  pdf_document:
    fig_caption: yes
---
```{r}
mycaption <- "This is my 
very long caption
that spans over
several lines.
(in the editor)"
```

```{r, fig.cap = mycaption}
plot(1)
```

使用选项eval.after,甚至可以在使用它作为选项值的块中定义mycaption

---
output: 
  pdf_document:
    fig_caption: yes
---
```{r}
library(knitr)
opts_knit$set(eval.after = "fig.cap")
```

```{r, fig.cap = mycaption}
mycaption <- "This is my 
very long caption
that spans over
several lines.
(in the editor)"

plot(1)
```

(我假设问题是代码在编辑器中看起来与输出中的换行符无关。)