kableExtra only works if there's another table

2020-07-11 07:19发布

When I use kableExtra for a PDF output, the table renders beautifully, BUT gives an error if there isn't another, non-kable extra table in the program. Has anyone else seen this behavior? the file appears to knit fine, but then throws an error in pandoc?

For example, this code:

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

Gives this error

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

But if I add one more slide with a dummy table, it prints beautifully:

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

Has anyone else seen this behavior? Any workarounds besides "put a dummy table as the last slide?"

1条回答
叼着烟拽天下
2楼-- · 2020-07-11 07:40

Beamer slides does not allow kableExtra to load latex packages automatically as it does in regular pdf document. You saw that error message from LaTeX because booktabs was not loaded. When you put an normal markdown table, some magic in rmarkdown pandoc template will load booktabs & longtable automatically, that's why the error was gone.

You can follow the documentation in the Getting Started section of the kableExtra documentation and put

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

Note that I removed - \usepackage[table]{xcolor} from the list because beamer loaded xcolor with a different option settings.

查看更多
登录 后发表回答