2

I try publishing a table in a pdf format using the gtsummary package in Rmarkdown. The code in Rmarkdown is as below:

---
title: "test"
author: "Tengku Hanis"
date: "`r Sys.Date()`"
output: pdf_document
---

```{r pressure, echo=FALSE}
mod <- lm(mpg ~ ., data = mtcars)
summary(mod)

```

```{r message=FALSE, error=FALSE, warning=FALSE}
library(tidyverse)
library(gtsummary)
```
    
```{r}
tbl_regression(mod, intercept = TRUE) %>% as_gt()
```

When I try to knit, I get the following error:

! Undefined control sequence.
<argument> ...al \expandafter \let \cmrsideswitch 
                                                  \@tempa \fi \fi 
l.164 (Intercept)
                  & 12 & -27, 51 & 0.5 \\ 

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

Is there any way to publish a table using tbl_regression() in pdf format while setting the intercept = TRUE? I think this argument somehow in conflict with as_gt()?

  • Use `keep_tex: true` see [here](https://stackoverflow.com/a/37702315/6574038) and in the .tex file check/edit lines around 164 by hand. It's probably related to numbers format. Search for `siunitx`, it's a great LaTeX package to align numbers. – jay.sf Nov 06 '22 at 09:23
  • Thanks for reporting this issue. I don't have the time to look into the details at the moment to assess if this is an issue with gt+PDF (it's a relatively new feature), or if the problem is with gtsummary to PDF via gt. In the meantime you can `as_kable_extra()` instead of `as_gt()` for PDF output. This blog post has details on gtsummary + kableExtra https://www.pipinghotdata.com/posts/2022-01-24-report-ready-pdf-tables-with-rmarkdown-knitr-kableextra-and-latex/ – Daniel D. Sjoberg Nov 06 '22 at 14:09
  • You can subscribe to this github issue to be notified when it is resolved. https://github.com/ddsjoberg/gtsummary/issues/1380 – Daniel D. Sjoberg Nov 06 '22 at 14:10
  • 1
    @DanielD.Sjoberg Thanks, the alternative solution works. – Tengku Hanis Nov 06 '22 at 22:50
  • Hi, @DanielD.Sjoberg, is this resolved already? I am facing the same problem. Knitting to word or html is just fine, knitting to pdf gets `failed to compile latex. ! Undefined control sequence....` – GuedesBF Jun 26 '23 at 19:15
  • I'm not sure. Follow the link to the issue and check its status @GuedesBF – Daniel D. Sjoberg Jun 26 '23 at 23:15

0 Answers0