kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

Inline latex code not showing in R Markdown Notebook when using latex_options

Open isaactpetersen opened this issue 4 years ago • 3 comments

Describe the bug When I specify latex_options from the kableExtra package in my R Markdown notebook, my inline latex code does not show after I run all code chunks. I've tried viewing it in Rstudio Preview or viewing the html in Google Chrome, but neither works (I'm not talking about viewing it in Rstudio's editor). When I remove the latex_options line, my inline latex code shows correctly. When I render using rmarkdown::render() (instead of running all code chunks in Rstudio), my inline latex code shows correctly. How can I get my inline latex code to show when using specifying latex_options from the kableExtra package?

To Reproduce Here's a reproducible example where the inline latex code does not show correctly—make sure to run all code chunks before previewing:

---
title: "R Notebook"
output: html_notebook
---

```{r}
library("kableExtra")
```

Table with latex options from kableExtra package:

```{r}
kable(head(mtcars)) %>%
  kable_styling(latex_options = c("scale_down"))
```

This works:

$$
x=\frac{1}{2}
$$

This does not work:

$x=\frac{1}{2}$

Here's a reproducible example where the inline latex code shows correctly:

---
title: "R Notebook"
output: html_notebook
---

```{r}
library("kableExtra")
```

Table with latex options from kableExtra package:

```{r}
kable(head(mtcars))
```

This works:

$$
x=\frac{1}{2}
$$

This also works:

$x=\frac{1}{2}$

Here's my sessionInfo():

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

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

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

other attached packages:
[1] kableExtra_1.3.1

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13   knitr_1.30        xml2_1.3.2        magrittr_2.0.1    rvest_0.3.6       munsell_0.5.0     colorspace_2.0-0 
 [8] viridisLite_0.3.0 R6_2.5.0          rlang_0.4.9       stringr_1.4.0     httr_1.4.2        highr_0.8         tools_4.0.2      
[15] webshot_0.5.2     packrat_0.5.0     xfun_0.19         htmltools_0.5.0   fortunes_1.5-4    yaml_2.2.1        digest_0.6.27    
[22] lifecycle_0.2.0   glue_1.4.2        evaluate_0.14     rmarkdown_2.6     stringi_1.5.3     compiler_4.0.2    scales_1.1.1     
[29] jsonlite_1.7.2

Here's my Rstudio version:

> rstudioapi::versionInfo()
$citation

To cite RStudio in publications use:

  RStudio Team (2020). RStudio: Integrated Development Environment for R. RStudio, PBC, Boston, MA URL
  http://www.rstudio.com/.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {RStudio: Integrated Development Environment for R},
    author = {{RStudio Team}},
    organization = {RStudio, PBC},
    address = {Boston, MA},
    year = {2020},
    url = {http://www.rstudio.com/},
  }


$mode
[1] "desktop"

$version
[1] ‘1.3.1056’

$release_name
[1] "Water Lily"

isaactpetersen avatar Dec 21 '20 16:12 isaactpetersen

@isaactpetersen the rmarkdown document you were working on was a html document. Using the code you provided, it seems that it's working on my machine.

Screen Shot 2020-12-23 at 11 49 46 PM

haozhu233 avatar Dec 24 '20 04:12 haozhu233

I have the same issue reported by @isaactpetersen and can confirm the same behavior on my machine. Inline equations are not displayed when previewing the html_notebook, using RStudio "Preview" button, but are displayed when rmarkdown::render() is used.

The output is also rendered correctly when the document is knitted to pdf or html. Moreover, the issue persists if we change

kable(head(mtcars)) %>%
kable_styling(latex_options = c("scale_down"))

with

kbl(head(mtcars))

My session info is:

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] kableExtra_1.3.4 pacman_0.5.1    

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13   knitr_1.31        xml2_1.3.2        magrittr_2.0.1    rvest_1.0.0      
 [6] munsell_0.5.0     colorspace_2.0-0  viridisLite_0.3.0 R6_2.5.0          rlang_0.4.10     
[11] highr_0.8         stringr_1.4.0     httr_1.4.2        tools_4.0.4       webshot_0.5.2    
[16] xfun_0.22         jquerylib_0.1.3   htmltools_0.5.1.1 systemfonts_1.0.1 yaml_2.2.1       
[21] digest_0.6.27     lifecycle_1.0.0   sass_0.3.1        rsconnect_0.8.16  glue_1.4.2       
[26] evaluate_0.14     rmarkdown_2.7     stringi_1.5.3     bslib_0.2.4       compiler_4.0.4   
[31] scales_1.1.1      jsonlite_1.7.2    svglite_2.0.0

and my Rstudio version is:

$citation

To cite RStudio in publications use:

  RStudio Team (2021). RStudio: Integrated Development Environment for R. RStudio, PBC, Boston, MA
  URL http://www.rstudio.com/.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {RStudio: Integrated Development Environment for R},
    author = {{RStudio Team}},
    organization = {RStudio, PBC},
    address = {Boston, MA},
    year = {2021},
    url = {http://www.rstudio.com/},
  }


$mode
[1] "desktop"

$version
[1] ‘1.4.1106’

$release_name
[1] "Tiger Daylily"

agutieda avatar Mar 18 '21 19:03 agutieda

I see. It was because I defined a customed mathjax syntax here https://github.com/haozhu233/kableExtra/blob/a6af5c067c2b4ca8317736f4a3e6c0f7db508fef/R/print.R#L14. It seems that preview treats the output as a regular rstudio browser so that's why it's not working.

haozhu233 avatar Mar 19 '21 05:03 haozhu233