S J*_*tan 3 r knitr r-markdown bookdown
我在 RStudio 中使用 knitr 来编写一个 rmarkdown bookdown:pdf:document2 文档。我有两个图,与 gridExtra 并排绘制,并标记为 A 和 B。我想在图标题的输出中放置一个换行符,如 fig.cap 所定义,在 A 的标题和 B 的标题之间,但我很难过。我试过了:
\n - 被忽略,就好像它不存在一样
\\n - 未定义的控制序列
\\\n - @tempf 的参数有一个额外的}。
\\\\n - 打印“\n”(这里有点傻)
双倍空间 - 什么都不做
出于绝望,我什至尝试了 HTML 样式的换行符,我不知道如何在此处显示,但我没想到它们会起作用,但它们没有。
在 LaTeX 中是可能的,所以肯定有办法......
注意:这不是knitr 中跨行拆分 r 块头的副本,因为它询问如何在代码中跨行拆分块头中的长标题,我问的是如何在输出中执行此操作。
苏珊娜
---
title: "MRR captions"
author: "Susannah Cowtan"
date: "14 December 2018"
output:
bookdown::pdf_document2:
citation_package: natbib
number_sections: no
toc: no
keep_tex: true
bookdown::html_document2: null
header-includes:
- \usepackage{float}
- \usepackage{booktabs}
fontsize: 11pt
papersize: A4
---
```{r knitr_setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r plot-mtcars, fig.height = 3, fig.width = 4, fig.cap = "A: foo bar baz \nB: foobar"}
plot(mpg ~ wt, data = mtcars)
```
Run Code Online (Sandbox Code Playgroud)
您可以通过插入适当的 LaTeX 命令来做到这一点,但在我看来,输出不是很令人愉快。
通过添加- \usepackage{caption}
到 header-includes 来包含标题包,然后\newline
在标题中使用该命令。
```{r plot-mtcars, fig.height = 3, fig.width = 4, fig.cap = "A: foo bar baz \\newline{}B: foobar"}
plot(mpg ~ wt, data = mtcars)
```
Run Code Online (Sandbox Code Playgroud)
添加足够的水平空白也会导致换行。但是,标题将不再显示为居中。
```{r plot-mtcars, fig.height = 3, fig.width = 4, fig.cap = "A: foo bar baz \\hspace{\\textwidth}B: foobar"}
plot(mpg ~ wt, data = mtcars)
```
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅TeX 堆栈交换。
您可以考虑使用子图代替换行符,例如
---
title: "MRR captions"
author: "Susannah Cowtan"
date: "14 December 2018"
output:
bookdown::pdf_document2:
keep_tex: true
header-includes:
- \usepackage{subfig}
---
See Figure \@ref(fig:plot-cars), which contains Figure \@ref(fig:plot-cars1) and Figure \@ref(fig:plot-cars2).
```{r plot-cars, fig.height = 3, fig.width = 4,, out.width='49%', fig.cap='Two plots', fig.subcap = c('foo bar baz', 'foobar')}
plot(mpg ~ wt, data = mtcars)
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)