Bin
Bin

Reputation: 547

how to wrap the caption in r pander function when knitting rmarkdown pdf

---
title: '1'
author: '1'
date: "March 16, 2017"
output:
  pdf_document: default
---

```{r cars}
library(pander)
pander(mtcars, split.cells = 5, split.table = Inf,
      caption = "A long long long long long long long long long long 
                 too long long long long long long long long caption")
```


```{r}
library(pander)
pander(mtcars, split.cells = 5, split.table = Inf,
      caption = paste("A long long long long long long long long long long", 
                 "too long long long long long long long long caption", 
                 collapse = "\n") )
```

enter image description here

I have tried paste function with collapse = "\n", but it fails.

I wish to (1) have a caption that wrap before the word "too" and (2) also center the caption rather than aligning the caption to the left.

Upvotes: 0

Views: 907

Answers (1)

Weihuang Wong
Weihuang Wong

Reputation: 13128

A slightly clumsy way is to manually adjust the caption margins so that the caption breaks where you want it to break. Include \usepackage{caption} in your YAML header, then set the caption margin before your table. You'll need to reset it before your next table or figure if you don't want the caption to break in the same way.

---
title: '1'
author: '1'
date: "March 16, 2017"
output:
  pdf_document: 
    keep_tex: true
header-includes:
   - \usepackage{caption}
---

\captionsetup{justification=centering,margin=3.5cm}
```{r}
library(pander)
pander(head(mtcars), split.cells = 5, split.table = Inf,
      caption = "A long long long long long long long long long long too long long 
      long long long long long long caption")
```

Output:

enter image description here

Upvotes: 1

Related Questions