Plotly: LaTeX Typesetting in R doesn't work

Created on 26 Dec 2015  路  10Comments  路  Source: ropensci/plotly

I can't reproduce an example from https://plot.ly/r/LaTeX/ with latest versions of R, RStudio and all packages on Windows (including development version of plotly). It looks like something wrong with MathJax support in the current version of package.
I have provided some screenshots in my question on Russian StackOverflow: http://ru.stackoverflow.com/questions/478133/latex-%D0%B2-plotly-r/

Most helpful comment

Any updates on this? I'm a big fan of ggplot2 and plotly, but it's a real shame to lose figure titles, axis labels, and/or legends that use plotmath expressions when attempting to add interactive features with ggplotly. Workaround hacks very welcome.

All 10 comments

Should be fixed if add mathjax library to the package and htmlwidgets config.

Mathjax is too big to bundle with the R package, so we're looking at a situation similar to #302, where we want to conditionally download/include relevant stuff under https://github.com/plotly/plotly.js/tree/master/dist before rendering

The thing is, with the current interface, there is no way to detect whether MathJax is needed. Maybe we provide a simple function, say

tex <- function(x) {
  structure(x, class = "LaTeX")
}

which could then be used essentially as a flag to include MathJax:

plot_ly(x = c(1, 2, 3, 4), y = c(1, 4, 9, 16),
        name = tex("\\alpha_{1c}"))

Any thoughts @chriddyp @cldougl @etpinard?

@timelyportfolio a while ago I tried to resolve this in #411. I'm sure you can come up with a better approach :)

Would it be reasonable to expect RStudio and rmarkdown availability? If so, perhaps we could leverage the rmarkdown:::pandoc_mathjax_local_path() function or just educate users on mathjax: local option in html_document. Then we wouldn't have to worry about doing all that RStudio does (see lines).

So, this works for me

``````

title: "MathJax Test"
runtime: shiny
output:
html_document:

mathjax: local

$$
\frac{d}{dx}\left( \int_{0}^{x} f(u)\,du\right)=f(x).
$$

```{r, echo=FALSE} 
sliderInput("x", label = "Value:",  min = 1, max = 10, value = 5)

renderText({
   input$x
})
```

``````

To confirm local, here is a screenshot of the network request.

image

Then the trick becomes how do we know to add mathjax if not in rmarkdown. We could employ a method like crosstalk to add dependencies with a add_mathjax() function.

That would certainly be nice if we could let rmarkdown do its thing in Rmd docs, but unfortunately, plotly.js doesn't seem play nicely.

``````

title: "MathJax Test"
output:
html_document:
self_contained: false

mathjax: local

$$
\frac{d}{dx}\left( \int_{0}^{x} f(u)\,du\right)=f(x).
$$

```{r, message = FALSE}
library(plotly)
plot_ly(x = 1:2, y = 1:2) %>%
layout(xaxis = list(title = "$x$"))

``````

> Would it be reasonable to expect RStudio and rmarkdown availability?

I wouldn't mind adding an rmarkdown dependency if need be, but I know there will be Jupyter/IPython/non-RStudio users that will want to generate static HTML that can be viewed locally.

We also need some way of including geoassets (#356), for local preview, [if a geo or choropleth trace type is used](https://github.com/ropensci/plotly/pull/411/files#diff-cbb7235afee10c3646161358a7655b0aR286). Unfortunately, due to the size of the external files, I think the best way to go about this is to require a clone of plotly.js, or at least a path to [its distributed files](https://github.com/plotly/plotly.js/tree/master/dist), and add them add dependencies when need be.

I was also thinking that, similar to rmarkdown, users should be able to specify whether or not they want to use local MathJax or the cdn, so something like:

``` r
plot_ly(x = 1:2, y = 1:2) %>%
  layout(xaxis = list(title = "$x$")) %>%
  config(mathjax = "local")

versus

plot_ly(x = 1:2, y = 1:2) %>%
  layout(xaxis = list(title = "$x$")) %>%
  config(mathjax = "cdn")

FWIW KaTeX may be a good alternative to Mathjax,

https://khan.github.io/KaTeX/

(one of its strengths is that it's light, self-contained and easy to bundle)

Any updates on this? I'm a big fan of ggplot2 and plotly, but it's a real shame to lose figure titles, axis labels, and/or legends that use plotmath expressions when attempting to add interactive features with ggplotly. Workaround hacks very welcome.

Similar discussion and solution proposal for the offline Python library:
https://github.com/plotly/plotly.py/issues/515

I'm likely going to create another R package, which will make easy to control plotly.js dependencies tied to R plotly objects, which will hopefully solve this and #356. The idea is to include just what you need from dist/ (and I might even do minimized bundles for each trace type to help minimize the file size)

Any companies out there interested in sponsoring this one?
https://plot.ly/products/consulting-and-oem/

@baptiste we looked at KaTeX, but there's still a lot of MathJax functionality that would need to be filled-in. Unfortunately it looks like the project is largely inactive right now.

Was this page helpful?
0 / 5 - 0 ratings