7

当我将 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)
```

输出

有没有其他人看到过这种行为?除了“将虚拟表作为最后一张幻灯片”之外的任何解决方法?

4

1 回答 1

12

Beamer slides 不允许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了不同的选项设置。

于 2017-11-15T20:02:22.407 回答