4

I'm using knitr in RStudio to write an rmarkdown bookdown:pdf:document2 document. I have two plots, plotted side-by-side with gridExtra, and labelled A and B. I want to put a newline in the output of the figure caption, as defined with fig.cap, between the caption for A and that for B, but I am stumped. I have tried:

\n - ignored as if it was not there

\\n - undefined control sequence

\\\n - Argument of @tempf has an extra }.

\\\\n - prints "\n" (getting a bit silly here)

double space - does nothing

I even tried, out of desperation, HTML style newlines, which I can't figure out how to display here, but I didn't expect them to work and they didn't.

It's possible in LaTeX so surely there is a way...

NOTE: this is not a duplicate of Split r chunk header across lines in knitr as that is asking how to split a long caption in a chunk header across lines in the code, and I am asking how to do so in the output.

Susannah

---
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)
```
S J Cowtan
  • 322
  • 3
  • 11
  • Have you tried a double space at the end of the line? It might be worth a try – p0bs Dec 19 '18 at 11:52
  • Edited to add that - still no :( – S J Cowtan Dec 19 '18 at 13:23
  • This looks like a duplicate of: – Pete Dec 19 '18 at 13:52
  • Possible duplicate of [Split r chunk header across lines in knitr](https://stackoverflow.com/questions/33628318/split-r-chunk-header-across-lines-in-knitr) – Pete Dec 19 '18 at 14:03
  • This is not a duplicate of https://stackoverflow.com/questions/33628318/split-r-chunk-header-across-lines-in-knitr as that is asking about splitting a long caption across lines in the code, and mine is asking about splitting it in the output. – S J Cowtan Dec 19 '18 at 15:00

2 Answers2

8

You can do this by inserting the appropriate LaTeX commands, but the output is, in my humble opinion, not very pleasant to look at.

Option 1: caption package

Include the caption package by adding - \usepackage{caption} to the header-includes, then use the \newline command in your caption.

```{r plot-mtcars, fig.height = 3, fig.width = 4, fig.cap = "A: foo bar baz \\newline{}B: foobar"}
plot(mpg ~ wt, data = mtcars)
```

Option 2: force a linebreak via a long line

Adding enough horizontal white-space will also cause a linebreak. However, the caption will no longer appear to be centered.

```{r plot-mtcars, fig.height = 3, fig.width = 4, fig.cap = "A: foo bar baz \\hspace{\\textwidth}B: foobar"}
plot(mpg ~ wt, data = mtcars)
```

See TeX stackexchange for details.

tarleb
  • 19,863
  • 4
  • 51
  • 80
8

Instead of a newline, you may consider using sub-figures, e.g.

---
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)
```

Sub-figures

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Note that this is only a suggestion, not intended to be a direct answer to the original question. @tarleb's answer is a direct one. – Yihui Xie Dec 19 '18 at 15:37
  • 1
    Even better, - \usepackage[margin = 8pt]{subfig} – S J Cowtan Dec 19 '18 at 15:56
  • I believe Yihui Xie's answer is much more helpful for future reader trying to solve the same problem. @SJCowtan, please consider marking this as the accepted answer instead. – tarleb Dec 19 '18 at 16:30