无法在 Rmarkdown/knitr beamer 演示文稿中插入 Latex

Trouble inserting Latex in Rmarkdown/knitr beamer presentation

我在从 Rmarkdown 文档编制 PDF 时遇到了问题。我已经查看了 ,但不太清楚如何将其应用到此上下文中。

这是我的 RMD "setup" 在 运行 代码之前。

以上是下面的 reprex 所需的 YAML 和库。这是一个 beamer 输出,当存在 ### TITLE 时默认为幻灯片级别,幻灯片内容由其下方的 R 块提供。有几个部分的问题。请让我知道我是否可以在 SO 上改进我的代表以更好地 ask/learn。

当我在 R 上使用 compareGroups 包时,它会创建一个乳胶输出。这适用于传统的 PDF,但不适用于 Beamer。我认为这与以某种方式让 rmarkdown 知道输出应该被保留并直接放在 Tex 文件中有关。但是,我不确定。

### Latex in context

```{r, echo=FALSE, results = 'asis'}
compareGroups(am ~ mpg, data = mtcars) %>%
  createTable() %>%
  export2latex()
```

输出将如下所示:

 \begin{longtable}{lccc}\caption{Summary descriptives table by groups of `am'}\
    \hline  
     &      0      &      1      & \multirow{2}{*}{p.overall}\ 
 &    N=19     &    N=13     &           \ 

    \hline
    \hline     
    \endfirsthead 
    \multicolumn{4}{l}{\tablename\ \thetable{} \textit{-- continued from previous page}}\ 
    \hline
     &      0      &      1      & \multirow{2}{*}{p.overall}\ 
 &    N=19     &    N=13     &           \ 

    \hline
    \hline  
    \endhead   
    \hline
    \multicolumn{4}{l}{\textit{continued on next page}} \ 
    \endfoot   
    \multicolumn{4}{l}{}  \ 
    \endlastfoot 
    mpg & 17.1 (3.83) & 24.4 (6.17) &   0.001  \ 
hp & 160 (53.9)  & 127 (84.1)  &   0.221  \ 
cyl & 6.95 (1.54) & 5.08 (1.55) &   0.002   \ 

    \hline
    \end{longtable} 

我更喜欢的是格式正确的 table 出现在最终文本中,但上面的原始乳胶出现了。我试过删除 {r, results="asis"} 评论,但它保持不变(在 Beamer 演示文稿中)。但是,我使用 export2md 命令时运气更好。当我如下使用它时,格式声明为降价,它确实在 PDF 中生成 table 。然而,它不是调整table。

### Markdown

```{r, echo=FALSE}
compareGroups(am ~ mpg, data = mtcars) %>%
  createTable() %>%
  export2md(format = "markdown")
```

当我使用乳胶格式时,编织完全失败。

### Latex

```{r, echo=FALSE}
compareGroups(am ~ mpg, data = mtcars) %>%
  createTable() %>%
  export2md(format = "latex")
```

在查看 .log 和 .tex 文件后,此输出似乎表明 \fnum@table 存在编号问题。我不知道足够的乳胶(或任何,真的)来解决这个问题。

output file: reprex-knitting.knit.md

! Undefined control sequence.
<argument> \fnum@table 
                       : 
l.9 ...mmary descriptives table by groups of `am'}
                                                  \ 

Error: LaTeX failed to compile reprex-knitting.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See reprex-knitting.log for more info.
Execution halted

我希望能够利用 compareGroups 包生成的乳胶,但请将 运行 保留在 beamer 演示文稿中。

感谢您的帮助。我认为这也是这个问题的正确地点。


最小工作示例:下面的代码块运行并重现编织到 beamer 时的错误(在 YAML header 中设置)。

---
title: "Knitting issues"
output: beamer_presentation
always_allow_html: true
header-includes:
  - \usepackage{longtable}
---

```{r, include=FALSE}
library(knitr)
library(rmarkdown)
library(magrittr)
library(compareGroups)
```
### Latex

```{r, echo=FALSE}
compareGroups(am ~ mpg, data = mtcars) %>%
  createTable() %>%
  export2md(format = "latex")
```

您可以使用这个小技巧使代码工作:

---
title: "Knitting issues"
output: beamer_presentation
always_allow_html: true
header-includes:
  - \usepackage{longtable,booktabs}
  - \makeatletter\def\fnum@table{\usebeamercolor{caption name}\usebeamerfont*{caption name}\tablename~\thetable}\makeatother
---

```{r, include=FALSE}
library(knitr)
library(rmarkdown)
library(magrittr)
library(compareGroups)
```
### Latex

```{r, echo=FALSE}
compareGroups(am ~ mpg, data = mtcars) %>%
  createTable() %>%
  export2md(format = "latex")
```