kableExtra 仅在演示文稿中有另一个 table 时才有效?

kableExtra only works if there's another table in the presentation?

当我将 kableExtra 用于 PDF 输出时,table 呈现得很漂亮,但如果程序中没有另一个非 kable extra table,则会给出错误。还有其他人看到过这种行为吗?该文件似乎编织得很好,但随后在 pandoc 中抛出错误?

例如,这段代码:

---
output: 
  beamer_presentation:
fontsize: 10pt
---

```{r global_options, include=FALSE}
library(rmarkdown)
library(knitr)
library(kableExtra)

```
### Slide with table
```{r echo=FALSE, warning=FALSE, message=FALSE}
df=mtcars[1:8,1:3]
kable(df,format="latex",booktabs=T,row.names=F) %>% 
    row_spec(6, color = "red") 
```

给出这个错误

processing file: t.rmd
List of 1
 $ include: logi FALSE

  |.......................................                          |  60%
  ordinary text without R code

  |....................................................             |  80%
label: unnamed-chunk-1 (with options) 
List of 3
 $ echo   : logi FALSE
 $ warning: logi FALSE
 $ message: logi FALSE

  |.................................................................| 100%
  ordinary text without R code


"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS t.utf8.md --to beamer --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output t.pdf --highlight-style tango --latex-engine pdflatex 
output file: t.knit.md

! Undefined control sequence.
\beamer@doifinframe ...in {tabular}{rrr} \toprule 
                                                  mpg & cyl & disp\ \midrul...
l.86 \end{frame}

pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS t.utf8.md --to beamer --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output t.pdf --highlight-style tango --latex-engine pdflatex' had status 43 
Execution halted

但如果我再添加一张带有虚拟 table 的幻灯片,它会打印得很漂亮:

---
output: 
  beamer_presentation:
fontsize: 10pt
---

```{r global_options, include=FALSE}
library(rmarkdown)
library(knitr)
library(kableExtra)
```
### Slide with table
```{r echo=FALSE, warning=FALSE, message=FALSE}
df=mtcars[1:8,1:3]
kable(df,format="latex",booktabs=T,row.names=F) %>% 
        row_spec(6, color = "red") 
```
### Non-kableExtra table needed for some reason?
```{r echo=FALSE}
kable(df)
```

还有其他人看到过这种行为吗?除了 "put a dummy table as the last slide?"

之外的任何解决方法

Beamer 幻灯片不允许 kableExtra 像在常规 pdf 文档中那样自动加载乳胶包。您从 LaTeX 看到了错误消息,因为未加载 booktabs。当你放置一个普通的 markdown table 时,rmarkdown pandoc 模板中的一些魔法会自动加载 booktabs & longtable,这就是错误消失的原因。

您可以按照 kableExtra 文档的 Getting Started 部分中的文档进行操作,然后将

header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}

请注意,我从列表中删除了 - \usepackage[table]{xcolor},因为 beamer 使用不同的选项设置加载了 xcolor