Evaluating LaTeX with Eddie and Alpha

If you work in LaTeX and write numerical equations,1 this post by Eddie Smith will cause all sorts of bells to go off in your head. He shows how Wolfram Alpha will evaluate an expression given to it in LaTeX form. In other words, you can paste something like this into the Alpha expression field

\frac{\sin{\pi/4} + \sqrt{2}}{1.625^2}

and Alpha will calculate it for you.

Alpha evaluating LaTeX

Unfortunately, the Result is presented as an image rather than text,2 so you can’t just select and copy it, but you can click the “Copyable plaintext” link to have Alpha pop up a text field that you can copy the answer out of.

As Eddie says, the advantage of this is that you don’t have to keep track of two parallel sets of equations: one in LaTeX for typesetting and the other in your calculator for getting the answers. The opportunities for a typo to sneak in are greatly reduced.

Even better than copying and pasting LaTeX expressions into a web page is to use some form of automation to do it for you. Eddie shows a simple Alfred workflow that cuts out a few steps, but I don’t use Alfred. Because I do all of my LaTeX writing in BBEdit, I wanted a way to jump to the computed answer straight from there.

The key is to recognize that the URL of Alpha page looks like this

http://www.wolframalpha.com/input/?i=%5Cfrac%7B%5Csin%7B%5Cpi%2F4%7D+%2B+%5Csqrt%7B2%7D%7D%7B1.625%5E2%7D

where everything after the ?i= is a URL-encoded version of the LaTeX expression you want to calculate. With that bit of information in hand, I wrote the following Python script to use as a BBEdit Text Filter:

python:
 1:  #!/usr/bin/python
 2:  
 3:  from urllib import quote_plus
 4:  from sys import stdin
 5:  from subprocess import call
 6:  
 7:  latex = stdin.read()
 8:  alphaURL = "http://www.wolframalpha.com/input/?i=%s" % quote_plus(latex)
 9:  print latex + ' = '
10:  call(['open', alphaURL])

The URL encoding is handled by the quote_plus function of the urllib module, and the web page is opened in the default browser through the magic of Apple’s open command.

I called the script Alpha Evaluate.py and saved it in BBEdit’s Text Filters folder. The Text‣Apply Text Filter submenu is kind of clumsy to negotiate, so I assigned it the keyboard shortcut ⌃=.

BBEdit keyboard shortcut for Text Filter

Now when I’m writing an equation, I can select it (which usually means hitting ⌘⇧← to select back to the beginning of the line) and type ⌃=. This puts an equals sign after the expression and opens Alpha in my browser. Within a second or two—and with no further work on my part—the expression is evaluated and waiting for me to copy and paste into my document.

I am, of course, not entirely satisfied with this solution. What I’d really like is to automate the copying and pasting of the answer. Wolfram’s page structure doesn’t make that easy, but it’s something I want to explore.

In the meantime, I have a new tool that will save me time and frustration. Thanks, Eddie!

Update 7/11/14
It’s nice to go to bed with an unfinished problem and wake up with solutions in your Twitter feed and inbox. The solution I found most appealing was Alex Chan’s. He used the Wolfram Alpha API to get an XML version of the Alpha output and extracted the plain text answer from that.

I can think of a couple of tweaks to Alex’s script that I’m going to try:

  1. Including the lines from my script that open the Alpha web page. Seeing the page will allow me to check that Alpha interpreted the LaTeX expression the way I expected.
  2. Passing a couple of extra query parameters to the API that will limit the mass of XML output to just the results I want. This isn’t strictly necessary, but I feel bad using bandwidth for results I discard.

Thanks, Alex!

Updated update 7/11/14
My tweak of Alex’s code can be found in this Gist. Turns out™ I had a Wolfram Alpha developer ID from some time ago but had completely forgotten about it. Your memory is the second thing to go.


  1. As opposed to symbolic. Lots of people do math with no numbers. 

  2. It’s an image because, in general, the result will be an equation or a graph or something else that isn’t normally expressed as plain text.