# Week 05 | KaTeX in R

## 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)

katex("x+1\\over\\sqrt{1-x^2}")

For one more, let’s do the probability of a coin toss example.

# using MathJax example
#   http://cdn.mathjax.org/mathjax/latest/test/sample.html
katex("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