# Convert hand-drawn equations to LaTeX with the mathpix package

September 29, 2017
By

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

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

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...