Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeX not compiling when "Intercept" is surrounded by () #50

Closed
andrewheiss opened this issue Mar 30, 2020 · 11 comments
Closed

LaTeX not compiling when "Intercept" is surrounded by () #50

andrewheiss opened this issue Mar 30, 2020 · 11 comments

Comments

@andrewheiss
Copy link

I'm seeing some strange behavior with current versions of modelsummary and gt. When knitting a document like this with LaTeX…

---
title: "Example"
output:
  pdf_document: 
    keep_tex: yes
---

```{r}
library(gt)
library(MASS)
library(modelsummary)

url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url) 
dat$Clergy <- ifelse(dat$Clergy > 40, 1, 0) # binary variable for logit model

models <- list()
models[['OLS 1']] <- lm(Literacy ~ Crime_prop + Infants, dat)
models[['NBin 1']] <- glm.nb(Literacy ~ Crime_prop + Donations, dat)
models[['OLS 2']] <- lm(Desertion ~ Crime_prop + Infants, dat)
models[['NBin 2']] <- glm.nb(Desertion ~ Crime_prop + Donations, dat)
models[['Logit 1']] <- glm(Clergy ~ Crime_prop + Infants, dat, family = binomial())
```

```{r}
msummary(models)
```

…I get this error about something with the (Intercept) line in the table:

! Undefined control sequence.
<argument> ...al \expandafter \let \cmrsideswitch 
                                                  \@tempa \fi \fi 
l.143 (Intercept)
                  & 64.114 & 4.218 & 57.331 & 4.384 & 1.006 \\ 

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

If I edit the generated .tex file and remove the parentheses around (Intercept), though, it compiles just fine:

\midrule
Intercept & 64.114 & 4.218 & 57.331 & 4.384 & 1.006 \\ 
 & (5.247) & (0.144) & (8.315) & (0.233) & (0.710) \\ 

image

@andrewheiss
Copy link
Author

It's only (Intercept) too: manually adding parentheses to other coefficient names works just fine

image

@andrewheiss
Copy link
Author

OH WAIT. You've already opened this issue at gt: rstudio/gt#145

@vincentarelbundock
Copy link
Owner

vincentarelbundock commented Mar 30, 2020

Yep, you found the problem @andrewheiss !

Howeeeever, I did put together a hack that should work until this is resolved upstream. If you look at the following portion of the README, you'll find example markdown documents that compile to pdf or tex, even with (Intercept):

https://github.com/vincentarelbundock/modelsummary#latex-output-and-dynamic-documents-with-knitr

Please let me know if that doesn't work for you.

@andrewheiss
Copy link
Author

That works great!

@capnrefsmmat
Copy link

This problem seems to have returned. Here's a simple test document:

---
title: Test Document
format: pdf
---

```{r}
library(modelsummary)

lm(disp ~ hp, data = mtcars) |>
  modelsummary()
```

I get the same LaTeX error when asking Quarto to render to PDF:

compilation failed- error
Undefined control sequence.
<argument> ...al \expandafter \let \cmrsideswitch 
                                                  \@tempa \fi \fi 
l.214 (Intercept)
                  & 20.992 \\ 

see test.log for more information.

I think this changed some time in the past 6 months, since last fall I worked on a set of lecture notes that used modelsummary extensively, and I believe I rendered them to PDF successfully after the time I started using modelsummary. But I may be mistaken. Was the workaround removed at some point?

@vincentarelbundock
Copy link
Owner

@capnrefsmmat can you make sure you are running a very recent version of LaTeX (and especially the siunitx package)?

I tried your example on my local computer and it worked in both Quarto and Rmarkdown. A similar example is also included in the test suite, which passes on continuous integration machines.

@capnrefsmmat
Copy link

I was on MacTeX 2022, so I upgraded to 2023, then used the TeX Live Utility to upgrade all the packages to their latest version. That puts me on siunitx version 3.2.6. I also used devtools::update_packages() to make sure I have the latest version of modelsummary and any dependencies. And I updated pandoc for good measure.

Nonetheless, I get the same error with an updated version:

running xelatex - 1
  This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex)
   restricted \write18 enabled.
  entering extended mode
  
updating tlmgr

updating existing packages

compilation failed- error
Undefined control sequence.
<argument> ...al \expandafter \let \cmrsideswitch 
                                                  \@tempa \fi \fi 
l.214 (Intercept)
                  & 20.992 \\ 

see test.log for more information.

That was by running quarto render test.qmd, on Quarto version 1.3.433.

I'm not sure what else to try. If it tells you anything, here's how the table appears in my TeX file:

\begin{longtable*}{lc}
\toprule
  & (1) \\ 
\midrule
(Intercept) & 20.992 \\ 
 & (32.607) \\ 
hp & 1.430 \\ 
 & (0.202) \\ 
Num.Obs. & 32 \\ 
R2 & 0.626 \\ 
R2 Adj. & 0.613 \\ 
AIC & 372.8 \\ 
BIC & 377.2 \\ 
Log.Lik. & -183.412 \\ 
F & 50.128 \\ 
RMSE & 74.64 \\ 
\bottomrule
\end{longtable*}

@vincentarelbundock
Copy link
Owner

Are you doing something weird to get a longtable environment? I’m not on my computer so I can’t check, but IIRC, that shouldn’t be the default…

@capnrefsmmat
Copy link

Not as far as I know -- I've always been getting longtable environments in my Quarto files.

Is the TeX for the table being generated by modelsummary, or does it generate a form that Quarto is converting (via Pandoc) to TeX? Because Pandoc automatically generates longtable by default.

@vincentarelbundock
Copy link
Owner

@capnrefsmmat Maybe you don't have the kableExtra package installed? Or you are not running the latest modelsummary? Or you have used config_modelsummary() to change something? Or are using options(modelsummary_factory_default) to change the style.

This is what I get. Notice the environment, and notice the \num{}

\begin{table}
\centering
\begin{tabular}[t]{lc}
\toprule
  & (1)\\
\midrule
(Intercept) & \num{30.099}\\
 & (\num{1.634})\\
hp & \num{-0.068}\\
 & (\num{0.010})\\
\midrule
Num.Obs. & \num{32}\\
R2 & \num{0.602}\\
R2 Adj. & \num{0.589}\\
AIC & \num{181.2}\\
BIC & \num{185.6}\\
Log.Lik. & \num{-87.619}\\
F & \num{45.460}\\
RMSE & \num{3.74}\\
\bottomrule
\end{tabular}
\end{table}

@capnrefsmmat
Copy link

Aha. I don't have kableExtra. That solves the problem. Thanks!

I see the documentation says we can use kableExtra to customize tables, but I don't see anywhere saying it's necessary for LaTeX tables to work reliably. Maybe that can be stated prominently somewhere? (Or it is and I don't know where to look...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants