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:
- Create new project with RStudio and choose "Book Project using bookdown" as option.
- Using default
_output.ymlandindex.Rmdrun code below in the01-intro.Rmdfile to show citations working correctly in two tables creating viakableExtra- one usingformat = "markdown"and one usingformat = "latex"(press "Build Book" and selectbookdown::pdf_bookin the RStudio "Build" tab) - Download a
.cslfile to change citation formathttps://raw.githubusercontent.com/citation-style-language/styles/master/nature.csl - Update
_output.ymlandindex.Rmdas shown below to specify the new.cslfile - Rerun code for the two tables. The citations should work in the
format = "markdown"version but not theformat = "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?