简体   繁体   中英

Trouble inserting Latex in Rmarkdown/knitr beamer presentation

I'm having trouble with knitting a PDF from an Rmarkdown document. I've reviewed this question , but couldn't quite figure out how to apply it to this context.

Here is my RMD "setup" prior to running the code.

Above is the YAML and libraries needed for the reprex below. This is a beamer output, with default to slide levels when there is a ### TITLE , with slide content by the R chunk below it. There are several sections of issues. Please let me know if I can improve my reprex to better ask/learn on SO.

When I use the compareGroups package on R, it creates a latex output. This works in a traditional PDF, but not for Beamer. I think that has to do with somehow letting the rmarkdown know that the output should be preserved and placed directly in the Tex file. But, I'm not sure.

### Latex in context

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

The output would look like this:

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

What I would prefer is that a properly formatted table appear in the final text, but the above raw latex appears instead. I've tried removing the {r, results="asis"} comment but that remains the same (in Beamer presentations). However, I've had better luck with the export2md command. When I use it as below, with format stated as markdown, it does make a table in the PDF. Its not adjustable however.

### Markdown

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

When I use a latex format, the knitting fails altogether.

### Latex

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

This output seems to suggest, after reviewing.log and.tex files, that there is a numbering issue with \fnum@table. I don't know enough latex (or any, really) to figure this out.

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

I would like to be able to leverage the latex that is generated by the compareGroups package, but keep running into this issue with the beamer presentation.

Thank you for your help. I think this is the right venue for this question as well.


Minimal Working Example: this code block below runs and reproduces the error when knitting to beamer (set in the 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")
```

You can make the code work with this little trick:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM