Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Statistics involves a lot of mathematics, so one of the nice things about report-generation systems for R like Rmarkdown is that it makes it easy to include nicely-formatted equations by using the LaTeX syntax.  So, if we want to include the density function of the Guassian Normal distribution:

$$\frac{1}{{\sigma \sqrt {2\pi } }} e^ { – \frac{ – \left( {x – \mu } \right)^2 }{2\sigma ^2} }$$

we can just add the following markup to the document:

$\frac{1}{{\sigma \sqrt {2\pi} }} e^{-\frac{-(x-\mu)^2}{2\sigma ^2}}$


Creating that markup can be a little tricky, but it generally follows naturally from the mathematics, and it's much easier than other methods like including a screenshot of the equation. Still, a new R package makes it even easier: the mathpix package will convert an image containing an equation to its LaTeX markup equivalent. That image might be a photo of a handwritten equation on paper of a whiteboard, or even a “stattoo”:

The resulting LaTeX isn't quite perfect: it mistakes the proportionality symbol for the Greek letter alpha (a mistake I've seen a few typesetters make). With that correction, the rendered equation — used for Bayesian inference — looks like:

$$p ( \theta | y) \propto p (y | \theta) p (\theta)$$

The mathpix package was created by Jonathan Carroll and is an interface to the Mathpix API. (It's recommended you sign up for a free API key if you intend to use this package regularly.) The package is available now on CRAN, and you can find more details on its Github page, linked below.

Github (jonocarroll): mathpix : Query the mathpix API to convert math images to LaTeX