Week05 - KaTeX Math in R
February 05, 2015
htmlwidgets News This Week
@jcheng5 from RStudio
-@jcheng5 - d3 bubble chart bubbles
demonstrated live in a simulated dashboard at this DataScience.LA meetup.
This Week’s Widget - katexR
| KaTeX rendering for R
I’m guessing your first question will be something like this.
With MathJax and LaTeX, why the #$&) do we need another math / formula renderer?
Well, the simple answer is speed, but unfortunately the need for speed requires a sacrifice for completeness, so choose with care (see this list for function support ). The speed boost is huge though. Here is a jsperf comparison run on my machine.
And, another comparison courtesy of IntMath.
Examples
Unfortunately, on this blog, I cannot post the result directly, so I have resorted to screenshots for the simple examples in this post. I’ll use the painful but typical <iframe>
for the later examples of katexR
in rmarkdown
.
note: you’ll need an extra \ to escape special characters
Simple Examples
# not on CRAN, so install with devtools
# devtools::install_github("timelyportfolio/katexR")
library(katexR)
katexR("x+1\\over\\sqrt{1-x^2}")
To see the similarity of KaTeX
with MathJax and LaTeX
, let’s use some examples from the MathJax examples. Here is the Cauchy-Schwarz Inequality
.
# using MathJax example
# http://cdn.mathjax.org/mathjax/latest/test/sample.html
katexR("\\left( \\sum_{k=1}^n a_k b_k \\right)^{\\!\\!2} \\leq
\\left( \\sum_{k=1}^n a_k^2 \\right) \\left( \\sum_{k=1}^n b_k^2 \\right)")
For one more, let’s do the probably of a coin toss example.
# using MathJax example
# http://cdn.mathjax.org/mathjax/latest/test/sample.html
katexR("P(E) = {n \\choose k} p^k (1-p)^{ n-k}")
In Markdown
I imagine that if you start using katexR
you’ll most likely want to use it in a rmarkdown
context for publication of research or presentations. I have embedded the rendered katexR
example below to prove that katexR
works just fine in rmarkdown
.
Thanks
Thanks so much for all the work by
- Khan Academy for educating all of us and open-sourcing their Javascript
- Ramnath Vaidyanathan and RStudio for
htmlwidgets