kableExtra 仅在演示文稿中有另一张桌子时才有效? [英] kableExtra only works if there's another table in the presentation?

查看:46
本文介绍了kableExtra 仅在演示文稿中有另一张桌子时才有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 kableExtra 用于 PDF 输出时,表格呈现精美,但如果程序中没有另一个非 kable 额外表格,则会出现错误.有没有其他人看到过这种行为?该文件似乎编织得很好,但随后在 pandoc 中引发错误?

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?

例如这段代码:

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

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

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

有其他人看到过这种行为吗?除了将虚拟表作为最后一张幻灯片"之外还有其他解决方法吗?

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

推荐答案

Beamer slides 不允许 kableExtra 像在常规 pdf 文档中那样自动加载乳胶包.您看到来自 LaTeX 的错误消息,因为没有加载 booktabs.当你放置一个普通的 Markdown 表格时,rmarkdown pandoc 模板中的一些魔法会加载 booktabs &longtable 自动,这就是错误消失的原因.

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.

您可以按照 kableExtra 文档的 Getting Started 部分中的文档并把

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}

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

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

这篇关于kableExtra 仅在演示文稿中有另一张桌子时才有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆