Citations in KableExtra table missing when format = "latex" and using custom csl

568 Views Asked by At

After updating a Bookdown project to include a custom .csl file for citations, citations included within a kableExtra table stop working but only when format = "latex" when creating a PDF book.

Steps to reproduce:

  1. Create new project with RStudio and choose "Book Project using bookdown" as option.
  2. Using default _output.yml and index.Rmd run code below in the 01-intro.Rmd file to show citations working correctly in two tables creating via kableExtra - one using format = "markdown" and one using format = "latex" (press "Build Book" and select bookdown::pdf_book in the RStudio "Build" tab)
  3. Download a .csl file to change citation format https://raw.githubusercontent.com/citation-style-language/styles/master/nature.csl
  4. Update _output.yml and index.Rmd as shown below to specify the new .csl file
  5. Rerun code for the two tables. The citations should work in the format = "markdown" version but not the format = "latex" version

Code for the first table (Markdown format)

Should work even after updating the .csl. This should be inserted as chunk in .Rmd file (01-intro.Rmd). Whilst this format creates citations correctly, text between columns often overlaps and columns can't be resized using kableExtra::column_spec() when using format = "markdown" in kable()

library(tibble)
library(dplyr)
library(knitr)
library(kableExtra)
tibble(`Column 1 Header` = c("Some fairly short text", "More similarly short text"),
           Column2 = c("A very very very very very very very very very very long sentence with citation[@xie2015]. And then another fairly wordy sentence.", "A different but also very very very very very very very very long sentence with different[@R-base]. And then another fairly wordy sentence.")) %>% 
      # mutate_all(linebreak, linebreaker = "breakbreakbreak") %>%
kable(format = "markdown",
      caption = "Table using Markdown Option")

Code for the second table (latex format)

This should be inserted in a second chunk. This creates citations correctly when using Bookdown defaults. After updating the .csl the citations appear as [?] - this is despite the citations appearing correctly elsewhere in the document.

tibble(`Column 1 Header` = c("Some fairly short text", "More similarly short text"),
           Column2 = c("A very very very very very very very very very very long sentence with citation\\cite{xie2015}. And then another fairly wordy sentence.", "A different but also very very very very very very very very long sentence with different\\cite{R-base}. And then another fairly wordy sentence.")) %>% 
      # mutate_all(linebreak, linebreaker = "breakbreakbreak") %>%
kable(format = "latex",
      caption = "Table using Latex Option",
      longtable = TRUE,
      booktabs = TRUE,
      escape = FALSE) %>% 
 column_spec(column = 1, width = "5cm") %>% 
 column_spec(column = 2, width = "8cm")

Code to directly download .csl file if not already present:

if(!file.exists("nature.csl")){
  download.file("https://raw.githubusercontent.com/citation-style-language/styles/master/nature.csl", 
                destfile = "nature.csl")
  }

_output.yml file with updated .csl

bookdown::gitbook:
  css: style.css
  config:
    toc:
      before: |
        <li><a href="./">A Minimal Book Example</a></li>
      after: |
        <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
    download: ["pdf", "epub"]
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  latex_engine: xelatex
  citation_package: none
  keep_tex: yes
bookdown::epub_book: default

YAML at top of index.Rmd after updating with .csl

---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib, packages.bib]
csl: nature.csl
link-citations: yes
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
---

Package Versions

bookdown: 0.22.3
knitr: 1.33
kableExtra: 1.3.4
markdown: 1.1
rmarkdown: 2.8
latex_engine: xelatex

Is there a way to use the custom .csl but still have functioning in-table citations with the \\cite{R-base} format for PDF outputs?

0

There are 0 best solutions below