我正在尝试使用 bookdown 将 .bib 文件中的引用包含到 Rmarkdown 中的 kable 表中。如果我使用 kableExtra::kbl()
在 kable 中指定 format = "markdown"
,引用就会起作用。但是将其提供给其他 kable_styling
格式选项不再起作用并会导致警告:
---
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()
```
这会导致生成的 pdf 中出现警告消息:
## 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
如果我排除 format = "markdown"
选项,警告就会消失,并且我会得到横向格式的正确表格,但引用不再起作用。
如何获取带有 kable_styling()
指定的选项的横向表以及表中的工作引用?
ref.bib的内容是:
@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},
}
最佳答案
警告告诉您 kable
的样式函数不适用于 markdown
格式,因此您需要使用 format = "latex"
(默认)确实如此。
为了使引用在表格中起作用,我们必须付出一些额外的努力,并且仅使用 LaTeX 来处理引用(而不是让 knit 预先进行一些争论)。
设置 .tex 文件
preamble.tex
其中包括合适的 LaTeX 包(如 natbib)并指定引用书目样式:序言.tex
\usepackage{natbib} \bibliographystyle{unsrtnat}
在 .Rmd YAML header 中包含序言:
output: bookdown::pdf_book: includes: in_header: preamble.tex
通过添加
\bibliography{ref}
包含引用书目在你的 .Rmd 中。使用\cite{<your_reference>}
以在文中引用。请注意\
是 R 的特殊字符,需要通过附加\
进行转义如果在字符串中使用:tmp <- data.frame( 'line' = c(1:3), 'ref' = c("no ref", "\\cite{R-base}", "no ref") )
示例:
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() ```
关于r - 如何使用 kbl format = "markdown"指定 kableExtra 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65296280/