rlatexr-markdownbookdown

! LaTeX Error: Environment threeparttable undefined. only when using bookdown to knit to pdf


I am creating a pdf document (my thesis) with rmarkdown. I am using the bookdown package to knit all chapters, using merge and knit approach. I have some complex tables that knit fine in a single chapter when I use the approach of knitting to pdf_document2 by clicking "Knit" for a single .Rmd file. But, when I try to knit multiple files I get this error: ! LaTeX Error: Environment ThreePartTable undefined.

This is my mwe: Knitting the following document works fine, and produces the expected result

---
header-includes:
- \usepackage{caption}
- \usepackage{pdflscape}
- \usepackage{longtable}
- \usepackage{lmodern}
- \usepackage{graphicx}
- \usepackage{threeparttable}
output:
  bookdown::pdf_document2:
    fig_caption: true
    number_sections: true
    toc: true
    toc_depth: '3'
    latex_engine: xelatex
fontsize: 11pt
always_allow_html: true
---

```{r setup2, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE)
library(readxl)
library(dplyr)
library(kableExtra)
library(stringr)
library(purrr)
```

# General Introduction

## Section

Text

```{r rable}

kbl(mtcars, booktabs = T, escape = F, caption = 'MT Cars')%>%
  kableExtra::footnote('(ref:longcap)', threeparttable =T)
```


(ref:longcap) A reaaaaaa aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaa aaaaaa aaaaaaa aaaaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaaaaaaaaa aaaa aaaaa aaaaaaaa aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaa aaaaaaaaa aaaaa aaaaaaaa aaaaaaaaaa aaaaaaa aaaaaaaaa aaaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa aaaaaaaaaaaaaaaally long caption with citations.


```{r rable2 }
iris%>%
  kbl( longtable = T, escape = F, booktabs =  T, caption = 'Table x')%>%
  column_spec(1, width = "8em")%>%
  column_spec(2, width = "10em")%>%
  column_spec(3, width = "9em")%>%
  column_spec(4, width = "10em")%>%
  column_spec(5, width = "12em")%>%
  kable_styling(latex_options= c("repeat_header"), font_size =10, repeat_header_method =  "replace")%>%
  kableExtra::footnote(general = '(ref:longcap)', 
           general_title =   "Abbreviations",
           threeparttable = T)%>%
  landscape()

```

However, when I use the bookdown method to combine multiple files, even just using the same code, I get the error. This is my _bookdown.yml:

index.Rmd is the YAML and code setup:

---
header-includes:
- \usepackage{caption}
- \usepackage{pdflscape}
- \usepackage{longtable}
- \usepackage{lmodern}
- \usepackage{graphicx}
- \usepackage{threeparttable}
output:
  bookdown::pdf_document2:
    fig_caption: true
    number_sections: true
    toc: true
    toc_depth: '3'
    latex_engine: xelatex
fontsize: 11pt
always_allow_html: true
---

```{r setup2, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE)
library(readxl)
library(dplyr)
library(kableExtra)
library(stringr)
library(purrr)
```

and testing2.Rmd is the chapter contents:


# General Introduction

## Section

Text



```{r rable}

kbl(mtcars, booktabs = T, escape = F, caption = 'MT Cars')%>%
  kableExtra::footnote('(ref:longcap)', threeparttable =T)
```


(ref:longcap) A reaaaaaa aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaa aaaaaa aaaaaaa aaaaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaaaaaaaaa aaaa aaaaa aaaaaaaa aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaa aaaaaaaaa aaaaa aaaaaaaa aaaaaaaaaa aaaaaaa aaaaaaaaa aaaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa aaaaaaaaaaaaaaaally long caption with citations.


```{r rable2 }
iris%>%
  kbl( longtable = T, escape = F, booktabs =  T, caption = 'Table x')%>%
  column_spec(1, width = "8em")%>%
  column_spec(2, width = "10em")%>%
  column_spec(3, width = "9em")%>%
  column_spec(4, width = "10em")%>%
  column_spec(5, width = "12em")%>%
  kable_styling(latex_options= c("repeat_header"), font_size =10, repeat_header_method =  "replace")%>%
  kableExtra::footnote(general = '(ref:longcap)', 
           general_title =   "Abbreviations",
           threeparttable = T)%>%
  landscape()

```

I then render with the following command: bookdown::render_book(".", "bookdown::pdf_document2", config_file = "_bookdown.yml", preview = F, clean = T)

My YAML and loaded packages are the same in the file run solo, and the index file. Why am I getting this error? The error occurs with the second table. Is it something to do with longtable?


Solution

  • The environment ThreePartTable isn't defined in the threeparttable style file, it's in the threeparttablex style. Adding

    - \usepackage{threeparttablex}
    

    to the header-includes in your index.Rmd file will fix the error.

    You're probably wondering why simply knitting your first file didn't need this. So am I, but my guess is that the knitting automatically included it, while the bookdown::render_book call clearly doesn't.

    Edited to add: I believe this is due to a bug in kableExtra that has been fixed in the devel version. The version on CRAN requires threeparttablex but doesn't force it to be used. The devel version fixes this bug (along with a number of others). You can install it using

    remotes::install_github("haozhu233/kableExtra")
    

    and after that you shouldn't need the \usepackage{threeparttablex} workaround given above.