如何使用 kbl format = "markdown" 指定 kableExtra 选项?
How do I specify kableExtra options with kbl format = "markdown"?
我正在尝试使用 bookdown 将 .bib 文件中的引文包含到 Rmarkdown 中的 kable table 中。如果我使用 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"
选项,警告就会消失,我得到正确的 table 横向格式,但引用不再有效。
如何使用 kable_styling()
指定的选项获得景观 table 和 table 中的工作引用?
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"
(默认值)。
为了使引用在 table 中起作用,我们必须付出一些额外的努力,并且只使用 LaTeX 来处理引用(而不是让 knitr 事先做一些争论)。
设置一个包含 suitable LaTeX packahe(如 natbib)并指定参考书目样式的 .tex 文件preamble.tex
:
preamble.tex
\usepackage{natbib}
\bibliographystyle{unsrtnat}
在您的 .Rmd YAML 中包含序言 header:
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
通过在您的 .Rmd 中添加 \bibliography{ref}
来包含参考书目。使用 \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()
```
我正在尝试使用 bookdown 将 .bib 文件中的引文包含到 Rmarkdown 中的 kable table 中。如果我使用 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"
选项,警告就会消失,我得到正确的 table 横向格式,但引用不再有效。
如何使用 kable_styling()
指定的选项获得景观 table 和 table 中的工作引用?
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"
(默认值)。
为了使引用在 table 中起作用,我们必须付出一些额外的努力,并且只使用 LaTeX 来处理引用(而不是让 knitr 事先做一些争论)。
设置一个包含 suitable LaTeX packahe(如 natbib)并指定参考书目样式的 .tex 文件
preamble.tex
:preamble.tex
\usepackage{natbib} \bibliographystyle{unsrtnat}
在您的 .Rmd YAML 中包含序言 header:
output: bookdown::pdf_book: includes: in_header: preamble.tex
通过在您的 .Rmd 中添加
\bibliography{ref}
来包含参考书目。使用\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() ```