When you knit the document, `knitr`

knits your `rmarkdown`

document to a `.tex`

document and then uses the latex engine to compile that into PDF. The latex code for your table is being output to the `.tex`

document, but then the compilation of the `.tex`

document to PDF fails with the error you're seeing.

According to the error message, the offending line in the `.tex`

document is line 165. If you save the tex document during compilation you can inspect it to see where the error is occurring. To save the `.tex`

document, use this in your yaml:

```
---
title: "mtcars2"
output:
pdf_document:
keep_tex: true
---
```

Inspecting the `.tex`

document, the offending line is:

`& cyl_factor: 6 cylinders (N = 7) & cyl_factor: 4 cylinders (N = 11) & cyl_factor: 8 cylinders (N = 14)\\`

My knowledge of latex is limited, but the problem appears to be the underscores, which are being interpreted as Math Mode commands to subscript the `f`

that follows the underscore. But Math Mode expressions have to be placed between `$`

, hence the error (for example, if you type `$x_i = 5$`

(outside of an R code chunk) in your document, you'll see that it compiles to a mathematical expression).

You can either remove or escape the underscores to get the document to compile to PDF, as shown in the example below. Escaping just means adding a double backslash `\\`

before the underscore to denote that the underscore is a literal underscore and not a latex command. There may be better approaches. Hopefully, someone with more latex knowledge will come along and show us other options that might be available.

```
---
title: "mtcars2"
output:
pdf_document:
keep_tex: true
---
```{r}
library(qwraps2)
library(dplyr)
library(knitr)
```
Here is a mathemtical expression: $x_i = 5$.
```{r}
# Remove underscore in cyl_factor
mtcars2 <-
dplyr::mutate(mtcars,
`cyl factor` = factor(cyl,
levels = c(6, 4, 8),
labels = paste(c(6, 4, 8), "cylinders")),
cyl_character = paste(cyl, "cylinders"))
our_summary1 <-
list("Miles Per Gallon" =
list("min" = ~ min(mpg),
"max" = ~ max(mpg),
"mean (sd)" = ~ qwraps2::mean_sd(mpg)),
"Displacement" =
list("min" = ~ min(disp),
"max" = ~ max(disp),
"mean (sd)" = ~ qwraps2::mean_sd(disp)),
"Weight (1000 lbs)" =
list("min" = ~ min(wt),
"max" = ~ max(wt),
"mean (sd)" = ~ qwraps2::mean_sd(wt)),
"Forward Gears" =
list("Three" = ~ qwraps2::n_perc0(gear == 3),
"Four" = ~ qwraps2::n_perc0(gear == 4),
"Five" = ~ qwraps2::n_perc0(gear == 5))
)
```
```{r, results='asis'}
summary_table(group_by(mtcars2, `cyl factor`), our_summary1)
```
```{r}
# Escape underscore in cyl_factor
mtcars2 <-
dplyr::mutate(mtcars,
`cyl\\_factor` = factor(cyl,
levels = c(6, 4, 8),
labels = paste(c(6, 4, 8), "cylinders")),
cyl_character = paste(cyl, "cylinders"))
our_summary1 <-
list("Miles Per Gallon" =
list("min" = ~ min(mpg),
"max" = ~ max(mpg),
"mean (sd)" = ~ qwraps2::mean_sd(mpg)),
"Displacement" =
list("min" = ~ min(disp),
"max" = ~ max(disp),
"mean (sd)" = ~ qwraps2::mean_sd(disp)),
"Weight (1000 lbs)" =
list("min" = ~ min(wt),
"max" = ~ max(wt),
"mean (sd)" = ~ qwraps2::mean_sd(wt)),
"Forward Gears" =
list("Three" = ~ qwraps2::n_perc0(gear == 3),
"Four" = ~ qwraps2::n_perc0(gear == 4),
"Five" = ~ qwraps2::n_perc0(gear == 5))
)
```
```{r, results='asis'}
summary_table(group_by(mtcars2, `cyl\\_factor`), our_summary1)
```
```

Just for illustration, you could also use Math Mode, which will cause the `f`

in `factor`

to be subscripted. That's not what you want, but just shows that the code also compiles properly if you add the appropriate Math Mode delimiters.

```
```{r}
# Use math mode
mtcars2 <-
dplyr::mutate(mtcars,
`$cyl_factor$` = factor(cyl,
levels = c(6, 4, 8),
labels = paste(c(6, 4, 8), "cylinders")),
cyl_character = paste(cyl, "cylinders"))
our_summary1 <-
list("Miles Per Gallon" =
list("min" = ~ min(mpg),
"max" = ~ max(mpg),
"mean (sd)" = ~ qwraps2::mean_sd(mpg)),
"Displacement" =
list("min" = ~ min(disp),
"max" = ~ max(disp),
"mean (sd)" = ~ qwraps2::mean_sd(disp)),
"Weight (1000 lbs)" =
list("min" = ~ min(wt),
"max" = ~ max(wt),
"mean (sd)" = ~ qwraps2::mean_sd(wt)),
"Forward Gears" =
list("Three" = ~ qwraps2::n_perc0(gear == 3),
"Four" = ~ qwraps2::n_perc0(gear == 4),
"Five" = ~ qwraps2::n_perc0(gear == 5))
)
```
```{r, results='asis'}
summary_table(group_by(mtcars2, `$cyl_factor$`), our_summary1)
```
```