yonicd/texPreview

"Missing $ inserted", "Fatal error occurred, no output PDF file produced!"

Joe-Wasserman opened this issue · 2 comments

I'm attempting to output an equatiomatic equation as an image that can be embedded in a README.md for github.

texPreview::tex_preview() seems to interpret the latex provided as invalid, tries to edit it, and then fails. I don't know enough about latex to know whether the latex is invalid and if it is, how to fix it.

I'm also not sure whether this is an error with {equatiomatic} or {texpreview}.

Reprex for the issue:

httr::GET("https://github.com/mymil/covid-19-united-states-county-quarterly-excess-deaths/blob/main/results/united_states_county_quarterly_model.RDS?raw=true",
    httr::write_disk(tf <- tempfile(fileext = ".RDS"))
)

lmer_model <- readRDS(file = tf)

equatiomatic::extract_eq(
  lmer_model,
  wrap = TRUE,
  terms_per_line = 2
) %>%
  texPreview::tex_preview(returnType = "html" )

Produces this error:

Error in tex_preview.default(obj, tex_lines, stem, overwrite, keep_pdf,  : 
   
! Missing $ inserted.
<inserted text> 
 $
l.8 \text{, for region_code j = 1,}
 \dots \text{,J} \\ 
!  ==> Fatal error occurred, no output PDF file produced!

The results of equatiomatic::extract_eq():

$$
\begin{aligned}
  \operatorname{total\_deaths\_per\_day}_{i}  &\sim N \left(\mu, \sigma^2 \right) \\
    \mu &=\alpha_{j[i],k[i],l[i]} + \beta_{1}(\operatorname{population\_z})\ + \\
&\quad \beta_{2}(\operatorname{year\_zero}) + \beta_{3}(\operatorname{quarter}_{\operatorname{2}})\ + \\
&\quad \beta_{4}(\operatorname{quarter}_{\operatorname{3}}) + \beta_{5}(\operatorname{quarter}_{\operatorname{4}}) \\
    \alpha_{j}  &\sim N \left(\mu_{\alpha_{j}}, \sigma^2_{\alpha_{j}} \right)
    \text{, for region_code j = 1,} \dots \text{,J} \\
    \alpha_{k}  &\sim N \left(\mu_{\alpha_{k}}, \sigma^2_{\alpha_{k}} \right)
    \text{, for county_set_code k = 1,} \dots \text{,K} \\
    \alpha_{l}  &\sim N \left(\mu_{\alpha_{l}}, \sigma^2_{\alpha_{l}} \right)
    \text{, for census_division l = 1,} \dots \text{,L}
\end{aligned}
$$
sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] lmerTest_3.1-3     lme4_1.1-26        Matrix_1.3-2       texPreview_1.5    
[5] tinytex_0.34       equatiomatic_0.3.0

loaded via a namespace (and not attached):
 [1] httr_1.4.2            svgPanZoom_0.3.4      jsonlite_1.7.2       
 [4] splines_4.0.4         here_1.0.1            rdocsyntax_0.4.1.9000
 [7] shiny_1.6.0           assertthat_0.2.1      statmod_1.4.35       
[10] numDeriv_2016.8-1.1   pillar_1.6.0          lattice_0.20-41      
[13] glue_1.4.2            digest_0.6.27         promises_1.2.0.1     
[16] rvest_1.0.0           minqa_1.2.4           colorspace_2.0-0     
[19] htmltools_0.5.1.1     httpuv_1.6.0          details_0.2.1        
[22] clipr_0.7.1           pkgconfig_2.0.3       magick_2.7.3         
[25] purrr_0.3.4           xtable_1.8-4          scales_1.1.1         
[28] whisker_0.4           later_1.2.0           tibble_3.1.0         
[31] generics_0.1.0        ggplot2_3.3.5         ellipsis_0.3.1       
[34] cachem_1.0.4          withr_2.4.2           magrittr_2.0.1       
[37] crayon_1.4.1          mime_0.10             memoise_2.0.0        
[40] fs_1.5.0              fansi_0.4.2           nlme_3.1-152         
[43] MASS_7.3-53           xml2_1.3.2            tools_4.0.4          
[46] lifecycle_1.0.0       V8_3.4.0              munsell_0.5.0        
[49] packrat_0.6.0         compiler_4.0.4        rlang_0.4.10         
[52] grid_4.0.4            nloptr_1.2.2.2        rstudioapi_0.13      
[55] base64enc_0.1-3       boot_1.3-26           gtable_0.3.0         
[58] DBI_1.1.1             curl_4.3              rematch2_2.1.2       
[61] R6_2.5.0              knitr_1.31            dplyr_1.0.5          
[64] fastmap_1.1.0         utf8_1.2.1            rprojroot_2.0.2      
[67] desc_1.3.0            Rcpp_1.0.7            vctrs_0.3.7          
[70] png_0.1-7             tidyselect_1.1.0      xfun_0.26  

error in equiomatic output: the underscores or not escaped in region_code and county_set_code

it should return this latex output in order for it to render

$$
\begin{aligned}
  \operatorname{total\_deaths\_per\_day}_{i}  &\sim N \left(\mu, \sigma^2 \right) \\
    \mu &=\alpha_{j[i],k[i],l[i]} + \beta_{1}(\operatorname{population\_z})\ + \\
&\quad \beta_{2}(\operatorname{year\_zero}) + \beta_{3}(\operatorname{quarter}_{\operatorname{2}})\ + \\
&\quad \beta_{4}(\operatorname{quarter}_{\operatorname{3}}) + \beta_{5}(\operatorname{quarter}_{\operatorname{4}}) \\
    \alpha_{j}  &\sim N \left(\mu_{\alpha_{j}}, \sigma^2_{\alpha_{j}} \right)
    \text{, for region\_code j = 1,} \dots \text{,J} \\
    \alpha_{k}  &\sim N \left(\mu_{\alpha_{k}}, \sigma^2_{\alpha_{k}} \right)
    \text{, for county\_set\_code k = 1,} \dots \text{,K} \\
    \alpha_{l}  &\sim N \left(\mu_{\alpha_{l}}, \sigma^2_{\alpha_{l}} \right)
    \text{, for state l = 1,} \dots \text{,L}
\end{aligned}
$$

Thank you!