How do I specify kableExtra options with kbl format = "markdown"?

1.9k Views Asked by At

I'm trying to include citations from a .bib file into a kable table in Rmarkdown, using bookdown. The citations work if I specify format = "markdown" in the kable using kableExtra::kbl(). But feeding this to additional kable_styling formatting options no longer works and results in warnings:

---
title: "Citation in landscape table"
site: bookdown::bookdown_site
output: 
  bookdown::pdf_book:
bibliography: [ref.bib]
biblio-style: apalike
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library('kableExtra')
```

```{r}
tmp <- data.frame('line' = c(1:3), 'ref' = c("no ref", "@R-base", "no ref"))
kbl(tmp, format = "markdown", longtable = TRUE, booktabs = TRUE, escape = FALSE) %>%
 kable_styling(latex_options =c("striped", "hold_position", "repeat_header", "scale_down")) %>%
 column_spec(2, width = "20em") %>%
 landscape()
```

This leads to warning messages in the pdf produced:

## Warning in kableExtra::kable_styling(., latex_options = c("striped",
## "hold_position", : Please specify format in kable. kableExtra can customize
## either HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ for
## details.
## Warning in column_spec(., 2, width = "20em"): Please specify format in
## kable. kableExtra can customize either HTML or LaTeX outputs. See https://
## haozhu233.github.io/kableExtra/ for details.
## Warning in kableExtra::landscape(.): Please specify format in kable. kableExtra
## can customize either HTML or LaTeX outputs. See https://haozhu233.github.io/
## kableExtra/ for details

If I exclude the format = "markdown" option the warnings disappear and I get the correct table in landscape format, but the citation doesn't work anymore.

How do I get both the landscape table with the options as specified with kable_styling() and working citations in the table?

The content of ref.bib is:

@Manual{R-base,
  title = {R: A Language and Environment for Statistical
           Computing},
  author = {{R Core Team}},
  organization = {R Foundation for Statistical Computing},
  address = {Vienna, Austria},
  year = {2019},
  url = {https://www.R-project.org},
}
1

There are 1 best solutions below

2
On BEST ANSWER

The warnings tell you that kable's styling functions don't apply to markdown format so you need to use format = "latex" (the default) indeed.

To make references work within the table we have to take some extra effort and use LaTeX only to handle citations (instead of letting knitr do some wrangling beforehand).

  1. Setup a .tex file preamble.tex that includes a suitable LaTeX packahe (like natbib) and specifies a bibliography style:

    preamble.tex

    \usepackage{natbib}
    \bibliographystyle{unsrtnat}
    
  2. Include the preamble in your .Rmd YAML header:

    output: 
    bookdown::pdf_book:
    includes: 
      in_header: preamble.tex
    
  3. Include the bibliography by adding \bibliography{ref} in your .Rmd. Use \cite{<your_reference>} to reference within the text. Note that \ is a special character to R and needs to be escaped by an additional \ if used inside a string:

    tmp <- data.frame(
     'line' = c(1:3), 
     'ref' = c("no ref", "\\cite{R-base}", "no ref")
    )
    

Example:

Let's check that citations work: R \citep{R-base} is great!

```{r}
kbl(tmp, booktabs = T, escape = F) %>%
 kableExtra::kable_styling(
   latex_options = c("striped", "hold_position", "repeat_header")
   ) %>%  
  column_spec(2, width = "50em") %>%
landscape()
```

enter image description here