0

I am writing a Rmarkdown document using bookdown::pdf_document2 for which I have a table that needs to do several very specific things, but I simply cannot find the solution to get all of them to work at the same time:

  1. The table needs to float to the end of the document as it would with the latex endfloat package.
  2. The table elements have Markdown bold and italics formatting that should appear correctly in the final PDF document
  3. One of the columns of the table has a lot of text that I would like to wrap within the table cell.

I have tried to get these three options to work with all sorts of combinations of knitr::kable, kableExtra::column_spec and pander, but I cannot find a way to get them all going at once. Below I am pasting an example Rmarkdown document with various tests, none of which fully works.

Is there a simple way to get the table to do what I want? Even a good workaround would be acceptable...

Thanks, David

---
title: "Test table formatting"
author: "David M. Kaplan"
date: "5/24/2020"
output: bookdown::pdf_document2
header-includes:
  - \usepackage[tablesfirst]{endfloat}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```

# Data

```{r}
df.long = data.frame(
  char = c('*this is some very long text with lots of words that will cause problems with word wrap if it is not properly handled with something like kableExtra*','**b**','~~c~~'),
  num = c(1,2,3))
```

# Kable with format=pandoc

```{r}
knitr::kable(df.long,caption="test1",format="pandoc")
```

**Result:** Handles formatting, but table does not float and no wordwrap.

# Kable with booktab

```{r}
knitr::kable(df.long,caption="test2",booktab=TRUE)
```

**Result:** Floats, but does not handle formatting or do wordwrap.


# kableExtra for wordwrap

```{r}
knitr::kable(df.long,caption="test3",booktab=TRUE) %>%
  kableExtra::column_spec(1,width="30em")
```


**Result:** Table floats and has wordwrap, but does not handle formatting.

# Pander

```{r}
pander::panderOptions("table.alignment.default","left")
pander::pander(df.long,caption="test4")
```

**Result:** Wordwrap and formatting, but does not float.
user3004015
  • 617
  • 1
  • 7
  • 14

1 Answers1

0

I found a solution to this problem. It basically consists of:

  1. In the YAML header, add a header-includes entry declaring longtable to be a float flavor using some LaTeX magic I found here.
  2. Use pander to format the table to get wordwrap

Specifically, I have the following in the YAML header:

header-includes:
  - \usepackage[tablesfirst]{endfloat}
  - \DeclareDelayedFloatFlavour*{longtable}{table}

and then the table can be generated with pander:

pander::panderOptions("table.alignment.default","left")
pander::pander(df.long,caption="test pander",split.cell=50)
user3004015
  • 617
  • 1
  • 7
  • 14