How to build a latex kable through bookdown::render_book?
Asked Answered
O

1

0

Here is a file kable.Rmd that knits properly in RStudio:

---
title: "latex kable"
output: pdf_document
---

```{r table1, echo=FALSE}
library(magrittr)
library(kableExtra)
kable(data.frame(a=c(1,2,3), b=c(4,5,6))) %>%
  kable_styling(latex_options = c("striped", "hold_position", "repeat_header"),
                full_width = F)
```

The output shows a table:

enter image description here

Here is a YAML file to configure bookdown called kable_bookdown.yml:

# See https://bookdown.org/yihui/bookdown/usage.html
rmd_files: ["kable.Rmd"]
delete_merged_file: true

Here is a call to render:

bookdown::render_book(input='kable.Rmd', config_file='kable_bookdown.yml')

Here is the error I get from that render:

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS _main.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output _main.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/lua/latex-div.lua --variable 'geometry:margin=1in'

! Undefined control sequence.
<recently read> \rowcolor 

The rowcolor likely comes from the "striped" latex option.

Why does it work in RStudio, but not through the render call?

RStudio seems to be using the same latex (pdflatex).

I'm using RStudio Version 1.2.5042, R 3.6.3, bookdown 0.18, kableExtra 1.1.0, knitr 1.28, tinytex 0.20.

EDIT: I was not able to get header_includes to work. Adding this to kable.Rmd worked for me:

header-includes:
- \usepackage{colortbl}
- \usepackage{xcolor}

EDIT 2: This also worked for me (from here):

output:
  pdf_document:
    # list latex packages:
    extra_dependencies: ["colortbl", "xcolor"]
Outsail answered 13/5, 2020 at 16:50 Comment(0)
C
1

This happens because kableExtra adds extra packages to the .tex header, and those are getting lost when bookdown does the processing. I believe \rowcolor comes from the colortbl package. So you need to tell bookdown to include that package. If I put this line into the YAML in kable.Rmd, it works:

header_includes: "colortbl"

but in a more complicated example, you may need more includes. Here are the ones that kableExtra causes to be inserted:

\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\usepackage{xcolor}
Colorblind answered 13/5, 2020 at 17:16 Comment(3)
Thanks! I could not get header_includes to work. See my EDIT above. If you change your answer to something that works for me, I'd be delighted to accept it.Outsail
Also: any ideas why bookdown acts differently from the "knit" render for these includes?Outsail
I'm seeing inconsistent results with both your edits and mine. Not sure what's going on.Colorblind

© 2022 - 2024 — McMap. All rights reserved.