rr-markdownxaringanequatiomatic

How to put an equatiomatic latex output into a xaringan slide?


I'm having trouble putting an equatiomatic formula output into a xaringan slide. For a standard rmd/quarto document, it's relatively simple. I follow the example on the package website and it works, but it doesn't seem to render in a xaringan document.

Take this very basic code for a xaringan slide:

---
output:
  xaringan::moon_reader:
    lib_dir: libs
---


```{r example-basic-preview, echo=FALSE}
library(equatiomatic)
mod1 <- lm(mpg ~ cyl + disp, mtcars)
extract_eq(mod1)
```

I can only get it to show the raw latex equation. I've tried numerous things like putting it on inline code, setting chunk option results = 'asis', cat()ing it with $$ before and after but I can't find the solution.


Solution

  • xaringan is a bit picky about math expression. Try in the following way,

    ---
    output:
      xaringan::moon_reader:
        lib_dir: libs
    ---
    
    ```{r example-basic-preview, echo=FALSE, results='asis'}
    library(equatiomatic)
    
    cat_eq <- function(...) cat("$$", extract_eq(...), "$$", sep = "")
    
    mod1 <- lm(mpg ~ cyl + disp, mtcars)
    cat_eq(mod1)
    ```
    

    xaringan slide with equatiomatic equation