更新Bookdown
项目以包含引文的自定义.csl
文件后,表格中包含的引文将kableExtra
停止工作,但仅format = "latex"
在创建 PDF 书籍时才起作用。
重现步骤:
- 使用 RStudio 创建新项目并选择“使用 bookdown 预订项目”作为选项。
- 在文件中使用默认值
_output.yml
并index.Rmd
运行下面的代码,01-intro.Rmd
以显示通过创建的两个表中的引文正常工作kableExtra
- 一个使用format = "markdown"
和一个使用format = "latex"
(按“构建书”并bookdown::pdf_book
在 RStudio“构建”选项卡中选择) - 下载
.csl
文件以更改引文格式https://raw.githubusercontent.com/citation-style-language/styles/master/nature.csl
- 更新
_output.yml
并index.Rmd
如下图指定新.csl
文件 - 重新运行这两个表的代码。引文应该在
format = "markdown"
版本中起作用,而不是在format = "latex"
版本中
第一个表的代码(Markdown 格式)
即使在更新.csl
. 这应该作为块插入.Rmd
文件(01-intro.Rmd
)中。kableExtra::column_spec()
虽然这种格式可以正确创建引文,但列之间的文本经常重叠,并且在使用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")
第二个表的代码(乳胶格式)
这应该插入到第二个块中。Bookdown
这会在使用默认值时正确创建引文。更新.csl
引文后显示为[?]
- 尽管引文在文档的其他地方正确显示。
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")
.csl
如果不存在,则直接下载文件的代码:
if(!file.exists("nature.csl")){
download.file("https://raw.githubusercontent.com/citation-style-language/styles/master/nature.csl",
destfile = "nature.csl")
}
_output.yml 文件,带有更新的 .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
index.Rmd
使用 .csl 更新后,顶部的 YAML
---
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."
---
包版本
bookdown:0.22.3
knitr:1.33
kableExtra:1.3.4
markdown:1.1
rmarkdown:2.8
latex_engine:xelatex
有没有办法使用自定义.csl
但仍然具有\\cite{R-base}
PDF 输出格式的表内引文?