vincentarelbundock / modelsummary

Beautiful and customizable model summaries in R.
http://modelsummary.com
Other
896 stars 74 forks source link

Caching in .rmd/.qmd throws `LaTeX Error: Environment talltblr undefined.` #775

Closed nicolaiberk closed 1 month ago

nicolaiberk commented 1 month ago

Bug reports must include:

  1. Concise description of the bug

Hi! Thank you so much for maintaining this package! When using modelsummary::modelsummary() in Quarto or RMarkdown, I run into a weird issue: with caching enabled, tables that were generated fine upon the first run are now throwing an error:

ERROR: 
compilation failed- error
LaTeX Error: Environment talltblr undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.207 \begin{talltblr}
                      [         %% tabularray outer open 
  1. Minimal reproducible example using publicly available data and the bare minimum code and libraries needed to reproduce the bug.

---
title: "Testing modelsummary 2.1.0"
format: pdf
execute: 
  cache: true
---

# Testing

```{r}

data(ChickWeight)

m1 <- lm(weight ~ Time, data = ChickWeight)
m2 <- lm(weight ~ Time + Diet, data = ChickWeight)

modelsummary::modelsummary(
  list(m1, m2),
  coef_map = list(
    Time = "Time (days)",
    Diet = "Diet"
  ),
  stars = TRUE)

(Generate document twice to produce error)

  1. sessionInfo() output
R version 4.4.0 (2024-04-24)
Platform: x86_64-apple-darwin20
Running under: macOS Ventura 13.5.2

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Zurich
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] modelsummary_2.1.0

loaded via a namespace (and not attached):
 [1] compiler_4.4.0    fastmap_1.1.1     generics_0.1.3    cli_3.6.2        
 [5] tools_4.4.0       htmltools_0.5.8.1 tables_0.9.25     knitr_1.46       
 [9] data.table_1.15.4 insight_0.19.11   xfun_0.44         digest_0.6.35    
[13] jsonlite_1.8.8    rlang_1.1.3 
tex -version
TeX 3.141592653 (TeX Live 2024)
kpathsea version 6.4.0
Copyright 2024 D.E. Knuth.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the TeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the TeX source.
Primary author of TeX: D.E. Knuth.

[x] Make sure you are running the latest development version of modelsummary and its dependencies: https://modelsummary.com/#installation

vincentarelbundock commented 1 month ago

Here's a guess: When compiling a Quarto, if you actually run modelsummary() command, it will add all the necessary LaTeX packages to your preamble automatically. If you cache the table, you do not run the command, and the necessary prerequisites are not added to the preamble. Thus, you need to add LaTeX commands (documented in the man page) to the preamble manually in the YAML.

nicolaiberk commented 1 month ago

Loading tabularray to header does indeed fix the issue - guess this should have been obvious. Thank you!

YAML:

---
title: "Testing modelsummary 2.1.0"
format: 
    pdf:
        include-in-header: 
            text: |
                \usepackage{tabularray}
execute: 
  cache: false
---