2

UPDATE: KableExtra creator haozhu233 added mathjax support to the package. See my original issue and his comments on Github: https://github.com/haozhu233/kableExtra/issues/473#issuecomment-668224096

When I try and create a table using kable and kableExtra I cannot get math symbols to display correctly. I am able to produce math characters in normal kable tables like expected. I've tried different variations of the options escape = FALSE and protect_latex = T without any luck. The math symbols display correctly if I knit to PDF or HTML, it's just the inline display (and preview) and notebook display that show the raw characters.

As far as I can see from the HTML output, the only difference between the kable table and the kableExtra table is that the latter contains some extra info on the class and style. The R objects are also of a different class, which makes me wonder if the problem might be that mathjax is not called to render kableExtra objects? If so, is there a way I can tell Rstudio to call on mathjax when rendering HTML from kableExtra objects?

I've reproduced this problem on two different computers and using the RStudio Cloud.

Note: i filed a Github Issue but realized that it might be more appropriate to ask here. I've also posted on the Rstudio community without getting a response so I thought I'd try my luck here.

Reprex:

---
title: "Kable Extra Math Symbols"
output: html_notebook
---

```{r}

math_symb <- c(1,2,3,4)

kable_table <- knitr::kable(math_symb, col.names = "$R^{2}$") 
styled_kable_table <- kableExtra::kable_styling(kable_table)

kable_table
styled_kable_table
```{}

From the example the object kable_table is rendered with the column name "R2", which is what I want to achieve whereas the styled_kable_table object is displayed with the the undesired column name "$R^2$"

Session Info:

R version 3.6.3 (2020-02-29) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale: 2 LC_COLLATE=Norwegian Bokmål_Norway.1252 LC_CTYPE=Norwegian Bokmål_Norway.1252 [3] LC_MONETARY=Norwegian Bokmål_Norway.1252 LC_NUMERIC=C [5] LC_TIME=Norwegian Bokmål_Norway.1252

attached base packages: 2 stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached): 2 Rcpp_1.0.4.6 rstudioapi_0.11 xml2_1.3.2 knitr_1.28 magrittr_1.5 hms_0.5.3 [7] munsell_0.5.0 rvest_0.3.5 viridisLite_0.3.0 colorspace_1.4-1 R6_2.4.1 rlang_0.4.6 [13] stringr_1.4.0 httr_1.4.1 highr_0.8 tools_3.6.3 webshot_0.5.2 xfun_0.14 [19] htmltools_0.4.0 ellipsis_0.3.0 yaml_2.2.1 digest_0.6.25 tibble_3.0.1 lifecycle_0.2.0 [25] crayon_1.3.4 kableExtra_1.1.0 readr_1.3.1 vctrs_0.3.0 glue_1.4.1 evaluate_0.14 [31] rmarkdown_2.1 stringi_1.4.6 compiler_3.6.3 pillar_1.4.4 scales_1.1.1 pkgconfig_2.0.3

mafw
  • 63
  • 6
  • Welcome to SO! To help us to help you, it's best to provide a simple, self-contained example that demonstrates your problem and can be used to test our solutions. [This post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) may be helpful. You've provided lots of context (well done!) but failed to provide any examples of which symbols are not printing in the way you wan them to. – Limey Jun 01 '20 at 09:20
  • Hello and thank you for your warm welcome and advice. I've edited my post to indicate the symbol which is not printing as I'd expect. – mafw Jun 01 '20 at 09:55

2 Answers2

2

This works:

```{r, results ='asis'}

math_symb <- c(1,2,3,4)

kable_table <- knitr::kable(math_symb, col.names = "$R^{2}$") 
styled_kable_table <- kableExtra::kable_styling(kable_table)

kable_table
cat(styled_kable_table)
```

enter image description here However, I have to investigate, why it works.

JAQuent
  • 1,137
  • 11
  • 25
  • I remember a similar issue that I had in a Shiny app. I had math symbols that weren't being rendered in a dynamic UI. It turned out that the problme was that wuthMathJax only rendered the initial version of the page, nit the dynamic rerendering. The solutiuon was to wrap the rerendering in `withMathJax` as well. My initial attempts to do the same with your problem have failed. But at least @JAQuent has given you a workaround. – Limey Jun 01 '20 at 10:39
  • 1
    This works for me too. To me this further suggests that mathjax might not called correctly on KableExtra objects since the raw html output from the `cat()` function is displayed correctly. – mafw Jun 01 '20 at 10:41
  • I agree with that assesment. Maybe worth re-opening that issue. – JAQuent Jun 01 '20 at 10:45
  • I slightly disagree. If you change your output to `html_document`, the `kable_styling()` renders correctly. But the `html_document` is not dynamic. I think it's the dynamic nature of the `html_notebook` that causes the problem, not `kable_styling`. [This page](https://docs.mathjax.org/en/v1.0/typeset.html) suggests a proper solution, but it's a lot more work than @JAQuent 's workaround! – Limey Jun 01 '20 at 11:02
  • I think that also sounds reasonable but that makes me wonder why the dynamic nature of the HTML notebook is not a problem for "normal" `kable` objects? To me this seems like a problem with how kntir, pandoc and mathjax work together to dynamically render html from `kableExtra` objects. That being said, I'm not sure which package maintainer I should raise this issue with... – mafw Jun 01 '20 at 11:09
0

You're producing an HTML document, so you need to format your header using HTML:

math_symb <- c(1,2,3,4)
math_symb %>%  kable(col.names = "R<sup>2</sup>")

Gives

enter image description here

Limey
  • 10,234
  • 2
  • 12
  • 32
  • Thank you for you suggestion. But mathjax in tandem with knitr and pandoc should take care of any HTML code so that I don't have to write strings using HTML. In my example, the kable table renders just fine without any HTML required. It's when I further process the table using the KableExtra package that the math fails to render correctly. – mafw Jun 01 '20 at 10:15
  • Ah! I see. My apologies. Curiously, your header `"$R^{2}$"` renders correctly for me even with `kable_styling()`. At the moment, I'm stumped... – Limey Jun 01 '20 at 10:23
  • Does it display correctly even in the viewer pane in Rstudio and if you preview the document as a HTML notebook? Because I get the desired output if I just knit to html or PDF. – mafw Jun 01 '20 at 10:25
  • No it doesn't display correctly in the preview pane, nor in an HTML notebook. But it does display correctly in an HTML document. – Limey Jun 01 '20 at 10:28
  • Ok, thanks for letting me know! Hopefully someone can chime in and explain why the preview pane (or notebook format) doesn't want to deal with kableextra objects... – mafw Jun 01 '20 at 10:32
  • I think I have the explanation (see my comment to @JAQuent 's solution) but I don't yet have a better solution! – Limey Jun 01 '20 at 10:41
  • Your explanation sounds reasonable as it corroborates my initial guess of mathjax being the culprit! I'll try and figure out a way to deal with this... – mafw Jun 01 '20 at 10:45