Convert hand-drawn equations to LaTeX with the mathpix package

By David Smith

(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 {2pi } }} e^ { – frac{ – left( {x – mu } right)^2 }{2sigma ^2} } $$

we can just add the following markup to the document:

frac{1}{{sigma sqrt {2pi} }} e^{-frac{-(x-mu)^2}{2sigma ^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”:

Mostly! Only propto that didn’t get recognized. Curious about results from @kopshtik & @emhrt_

— Mikhail Popov (@bearloga) September 27, 2017

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

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. 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…

Source:: R News

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.