kableExtra只有在演示文稿中有另一个表时才有效吗?

时间:2017-11-15 19:13:55

标签: r r-markdown kableextra kable

当我使用kableExtra进行PDF输出时,表格呈现得很漂亮,如果程序中没有另一个非kable额外表格,则会出错。有没有人见过这种行为?该文件似乎编织好,但然后在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

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

---
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)
```

output

还有其他人看过这种行为吗?除了“把假表作为最后一张幻灯片?”之外的任何变通方法。

1 个答案:

答案 0 :(得分:7)

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

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

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