Reputation: 21387
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:
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"]
Upvotes: 0
Views: 317
Reputation: 44788
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}
Upvotes: 1