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

Feature request: Inject LaTeX math into Markdown text by wrapping in $$, like R Markdown #4

Open
mattwarkentin opened this issue Aug 14, 2019 · 13 comments

Comments

@mattwarkentin
Copy link

mattwarkentin commented Aug 14, 2019

Hi,

This package is great and will undoubtedly save lots of people lots of headaches. I am wondering if you would be able to add additional support to be able to inject LaTeX math into the text for plots. I did notice you have shown an example of using HTML tags to do things like superscripts and subscripts, however, I think many users will be more familiar with writing more complex equations using LaTeX.

I really have no idea of the difficulty in adding this feature, but it would be great to use some sort of system like R Markdown uses, where it seems to parse the $$ and renders all text inside as LaTeX math. This would certainly be an amazing feature, in my eyes.

Something like:

iris %>%
  ggplot(aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  geom_smooth(method = 'lm') +
  labs(title = "The line in this plot represents $\hat{y}=\beta_0+\beta_1x_1$")

Thank you for your work.

@clauswilke
Copy link
Collaborator

I agree it's a useful feature, but it's a lot of work. (Essentially, somebody would have to re-implement large parts of the TeX rendering engine in R grid.) So don't get your hopes up for this to be coming soon or ever.

@mattwarkentin
Copy link
Author

Thanks for the reply. I suspected this would be a non-trivial feature. In any case, I am very thankful for your hard work and I am excited to use this package!

@baptiste
Copy link

Rather than re-implementing a TeX-like engine I think one could potentially leverage a javascript-based one such as KaTeX – with the existing R-js interpreters/bridges it might in fact be quite doable.

https://katex.org/docs/api.html#server-side-rendering-or-rendering-to-a-string

@clauswilke
Copy link
Collaborator

@baptiste Thanks for the pointer. How feasible this is depends on what kind of html tags it generates. Would you be in a position to provide example R code that goes from an input latex expression to output html? (E.g., the equivalent of katex.renderToString() but wrapped into R.)

@baptiste
Copy link

Not likely – too many things on.

@mattwarkentin
Copy link
Author

Hi @clauswilke,

So I felt some obligation to try and attempt this since I started the issue; but I didn't think I had the javascript knowledge to be able to do it. I did some reading and here's my first attempt. I have tried to satisfy your request of getting the output HTML using katex.renderToString() from inside R. This attempt is quite crude, since it is simply running javascript code inside of R, but it is a starting point. If this is even remotely on the right track for what you were hoping to see, I could wrap this into a more palatable R function.

Here goes:

  1. Install katex in working directory
npm install katex
  1. Followed this vignette to bundle the katex library into a stand-alone Javascript file:
npm install -g browserify

echo "global.katex = require('katex');" > in.js
browserify in.js -o bundle.js
  1. In R (with V8 package installed):
library(V8)
ct <- v8()
ct$source(file = "/Users/Matt/bundle.js")

ct$eval(
'var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}", {
    throwOnError: false
});')

ct$get('html')

And here is the resulting HTML string:

<span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>c</mi><mo>=</mo><mi>p</mi><mi>m</mi><mi>s</mi><mi>q</mi><mi>r</mi><mi>t</mi><mrow><msup><mi>a</mi><mn>2</mn></msup><mo>+</mo><msup><mi>b</mi><mn>2</mn></msup></mrow></mrow><annotation encoding=\"application/x-tex\">c = pmsqrt{a^2 + b^2}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.43056em;vertical-align:0em;\"></span><span class=\"mord mathdefault\">c</span><span class=\"mspace\" style=\"margin-right:0.2777777777777778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2777777777777778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:1.008548em;vertical-align:-0.19444em;\"></span><span class=\"mord mathdefault\">p</span><span class=\"mord mathdefault\">m</span><span class=\"mord mathdefault\">s</span><span class=\"mord mathdefault\" style=\"margin-right:0.03588em;\">q</span><span class=\"mord mathdefault\" style=\"margin-right:0.02778em;\">r</span><span class=\"mord mathdefault\">t</span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathdefault\">a</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141079999999999em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222222222222222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222222222222222em;\"></span><span class=\"mord\"><span class=\"mord mathdefault\">b</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141079999999999em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span></span></span></span></span>

@clauswilke
Copy link
Collaborator

Thanks! This will definitely require more CSS support than I currently have implemented in gridtext, so we'll have to defer for now.

@mattwarkentin
Copy link
Author

Alright, sounds good. Please let me know if there is anything else I can do to help down the line.

@baptiste
Copy link

@coolbutuseless might rendering this be easy in minisvg? Much like tikzGraphics can bypass the grDevices bottleneck, here one could perhaps leverage native mathml–xml support (noting that web technologies are increasingly becoming the most relevant graphics output)

@coolbutuseless
Copy link

@baptiste I think it could be possible. Could you file an issue on coolbutuseless/devoutsvg and link to here?

@zmeers
Copy link

zmeers commented Feb 8, 2020

Hi all,

I was searching for this feature in ggtext, but I understand it might take a while to set up or it's not a priority for the package devs. In the meantime, I've been working with latex2exp instead. While it's not as intuitive as the request posed above, primarily because you need to wrap the text arg in latex2exp::TeX("$\alpha$") - e.g. ggplot() + geom_text(aes(label = latex2exp::TeX("$\\alpha$") )), it does what we want. See the vignette here: https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html.

Hope this helps if people are looking for another way to input latex into ggtext!

@wdkrnls
Copy link

wdkrnls commented May 15, 2020

@mattwarkentin
Copy link
Author

Perhaps the new katex package will be useful if/when ggtext/gridtext supports more HTML tags.

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

6 participants