在Rmarkdownknitr梁的演示中插入Latex的问题

问题描述 投票:1回答:1

我在从Rmarkdown文档中编织PDF时遇到了麻烦。我已经回顾了 这个问题但我不知道如何将其应用到这个环境中。

下面是我在运行代码之前的RMD "设置"。

上面是下面的reprex所需要的YAML和库。这是一个beamer输出,默认为滑动级别,当有一个 ### TITLE,幻灯片内容由它下面的R块。有几个部分的问题。如果我可以改进我的reprex,以便更好地在SO上asklearn,请让我知道。

当我在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} 

我更希望在最终的文本中出现一个正确格式化的表格 但却出现了上面的原始乳胶。我试着把下面的 {r, results="asis"} 注释,但这仍然是相同的(在Beamer演示中)。然而,我使用 export2md 命令的运气更好。当我使用下面的命令时,在格式为markdown的情况下,它确实能在PDF中制作一个表格。然而,它是不可调整的。

### 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。我对latex(或任何,真的)的了解不够,无法弄清楚这个问题。

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

我希望能够利用由 "Latex "生成的 比较组 包,但一直遇到这个问题,梁子的表现。

谢谢您的帮助。我想这个问题也是合适的地点。


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

---
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")
```
r latex r-markdown knitr
1个回答
2
投票

你可以用这个小技巧让代码工作。

---
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")
```
© www.soinside.com 2019 - 2024. All rights reserved.