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

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:
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 submenu is kind of clumsy to negotiate, so I assigned it the keyboard shortcut ⌃=.

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.

# The personal touch

A couple of weeks ago, my old rant against stacked area charts got linked on Hacker News, resulting in a bunch of traffic and, somewhat surprisingly, generally favorable comments from the Hacker Newsies. I’m going to take this as evidence of my authority on data visualization—which we used to just call graphing—and inflict more of my opinions on you.

First, though, you should go read this wonderful post by Dr. Bunsen. The main topic is a pair of dynamic, interactive graphs he made from the split and final times of the top runners in the last five Boston Marathons. Each part of each graph is quite simple, but the way he combines the parts leads to a result that’s both dense in the data it presents and crystal clear in how it presents it. It’s a nice piece of work, and he gives you behind-the-scenes access to how it works through this GitHub repository.

Late in the post, Seth reveals the inspiration for his dynamic graphs: this chart by Henry Gannett from the 1890 Statistical Atlas of the United States.

It’s a timeline of state population ranking from census to census. You’ll get a better sense of how it works by looking at this detail from the right side of the chart.

1. There’s no key or other explanation. Everything you need to know can be found by reading the title and looking at the chart.
2. Gannett chose to run his time from right to left. I think he’s using our convention of reading from left to right to make us think of this as a stepwise look back through our past from today’s (1890) point of view.
3. The lozenges Gannett uses for the states. He can’t just use color—the gradations would be too fine and printing costs would skyrocket—so he combines color and pattern to make each state distinct and easy to follow back to its first appearance in the census.
4. The placement of the state labels is inconsistent, but purposely so. Most often, a state or territory’s first appearance is near the bottom of the ranking, so the name is off to the side of the lozenge. But in some instances—Kentucky and Vermont in 1800, for example—the state comes in near the middle and to putting its name off to the side would be messy. In those cases, the label goes in the lozenge.

It’s hard to imagine a chart made today looking like this because—in a graphical version of the Sapir-Whorf hypothesis—the software we use to make our charts directs and limits our design decisions. How often do you see timelines that run from right to left? Or patterns instead of color to distinguish items?1

And inconsistency, even inconsistency with a purpose, is antithetical to software-driven graphing. If you use graphing software a lot, or if there’s a graphing program or package that’s common in your business, I’ll bet you can instantly identify charts made with it because there is a sameness to them. That sameness isn’t necessarily a bad thing, but sometimes the conventions get in the way of communication. And there are ways to improve our graphs, to make them look less mechanical and to tell our stories more clearly.

The easiest way is to import a plot into a drawing program and add the sort of customization that used to be common back when graphs were done by hand. Tweak the positions and orientations of labels so they don’t interfere with each other or with the rest of the plot. Change the points of your scattergraph to a symbol that evokes the item that’s being plotted. Adjust the lengths of unlabeled tickmarks to make it easier to read the axis subdivisions (think of how rulers are marked). Tweak the widths of lines so the data are more prominent than the chrome around the edges of the plot. In general, think about what you wish you could do in your plotting program and do it “by hand” in a drawing program.

For years, I’ve been using OmniGraffle to enhance the graphs I put in my reports and presentations. It’s great for adding items like labels and arrows to a plot, but it’s limited in that a PDF you add to an OmniGraffle document comes in as a single monolithic object—you can draw on top of it, but you can’t change its internals. Recently I’ve been playing with iDraw, which is a little squirrelly but doesn’t have OmniGraffle’s limitation: when you import the PDF of a chart into iDraw, each object within the chart is individually selectable and editable. I can change, for example, the style of a line within the plot or grab an existing label and rotate it to match the slope of the trend line it’s captioning.

The harder way to make better graphs is to use a more powerful graphing program, preferably one that’s programmable or is a plotting package for a programming language you already know.2 This gives you more flexibility than a canned program like Excel or (God help you) Numbers in formatting the parts of your plot. It’s certainly harder to write a program that makes a plot than to just select a column of numbers and click a few options, but that time and effort is repaid when you need to make dozens of plots of the same type. Once that program is tweaked to output exactly what you want, you can apply it to dataset after dataset.

After many years of using Gnuplot, I switched to Matplotlib and have been very happy with the results. But I still do some tweaks “by hand” instead of in Matplotlib. If I know I’m only making one or two graphs and the tweaks I want to make are easy to do in iDraw, I’ll accept Matplotlib’s defaults and make the changes in iDraw. If, on the other hand, I know I’ll be making essentially the same plot over and over again, I’ll spend the time with the Matplotlib documentation to program in all the stylistic refinements so I can crank out plots with no effort later.

I confess that the plots I occasionally post here don’t get the attention suggested in the previous paragraphs. This might be because I know you’re all so smart you don’t that kind of hand holding. Or it might be because you don’t pay my bills.

1. In the early days of the Mac, patterns were common because that’s all we had on our little black-and-white screens and Bill Atkinson’s QuickDraw gave programmers easy access to many fill patterns. But those patterns were pretty ugly, and because they were bitmapped, they didn’t translate well to the LaserWriter.

2. Or you could go super hardcore and program your plots from drawing primitives, which seems to be what Seth did in his Boston Marathon graphs.

# Displaying multiple images in Twitter

The Tapbots crew beat me to it by a couple of days, but I did manage to update Dr. Twoot to handle Twitter’s new multiple image format. And I have to say I prefer my display of multiple images to both Tweetbot’s and Twitter’s.

This isn’t to say that Dr. Twoot is truly comparable to a full-featured Twitter client like Tweetbot, but the great advantage of making your own software is that you can customize it to match your own idiosyncrasies. Thus, Dr. Twoot

• displays tweets in chronological rather than reverse chronological order;
• uses background colors to distinguish tweets by me and replying to me from the general stream;
• gives me one-click blocking and spam reporting;
• shows the Twitter client used to write every tweet;
• has a popup for displaying information about the tweeter;
• has an edit (really delete-and-rewrite) button for fixing typos in tweets that have already been posted; and
• shows the full frame of included images instead of cropped versions.

Tweetbot follows Twitter’s own convention of displaying only a portion of the attached image unless the aspect ratio of the image happens to match the roughly 9:5 frame Tweetbot uses. For most images Tweetbot crops off the top and bottom, which often leaves you wondering what’s really there. Dr. Twoot, on the other hand, shows the whole image. It’s resized to fit in the space, but it’s all there. No need to open it in another window just to see what you’re missing.

In this, Tweetbot follows Twitter’s lead. On the Twitter web page, you see only cropped images until you click on the Expand button or the image itself.

There’s a certain neatness to the Twitter/Tweetbot approach. An individual tweet will never get so tall as to take up the entire window. Still, I prefer to see the whole image right away and am willing to sacrifice vertical uniformity to get it.

Similarly, in the display of multiple images, I prefer to show them full frame and stack them one on top of the other, even if that means I have to scroll to see them all.

Again, there’s nothing wrong with what Tweetbot does—it’s a perfectly valid way to lay things out and is a clever way to pack more images into a single frame. I just have a different preference. And because I’m Dr. Twoot’s only user, my preference wins.

Update 7/5/14
Both paullu and Stephen Hackett pointed out on Twitter that I could change Tweetbot’s Image Thumbnail setting from Large to Small, which changes the aspect ratio from 9:5 to 1:1 and makes the cropping of most images—and portrait images in particular—less severe. That’s true, but it makes the images too small for comfort, especially on the non-Retina screens of my iMac and MacBook Air. I like my images the way I like my men: large and fully framed.

# Extracting coordinates from Apple Maps

A couple of years ago, I wrote a script called coordinate that inserted latitude and longitude into the metadata of photographs I took with my regular, non-GPS-equipped, camera. I set it up to work two ways:

1. If I had a photo that already included GPS coordinates (one taken with an iPhone, for example), the script would extract that information and apply it to the non-GPS photos.
2. If I didn’t have such a photo, I could provide the latitude and longitude on the command line.

At the time, the easiest way for me to get latitude and longitude (if I didn’t have an iPhone photo with it) was to go to Google Maps, find the spot where the photos were taken, and right click on it. Both the Drop LatLng Marker (enabled through Maps Labs) and the What’s Here context menu items will show the coordinates.

The thing is, I’ve come to hate Google Maps. I know it has more and better information than Apple Maps, but its user interface has gotten worse over the years as Google has tried to squeeze more functionality into what is, when all is said and done, still just a web page. And I’m sick of having Google+ pushed at me.

So I’d prefer to get the coordinates from Apple Maps. Apple, of course, likes to hide such complicated technical information from its users, but there’s a way around the obfuscation. Here are the steps to get the latitude and longitude:

1. Right click to drop a pin at the spot you’re interested in.
2. Click the little info button on the pin’s flag.
3. Click the share button on the info window that pops up and choose from the context menu.
4. Right click on the Map link in the message window and choose from the context menu.

You now have a URL on your clipboard that looks something like this:

http://maps.apple.com/?q=41.772999,-88.150383&sspn=0.002885,0.006427&sll=41.772891,-88.149888


The coordinates of the pin in lat,long form is the value of the q field in the query string.

Even if I could remember this series of steps flawlessly, it still doesn’t give me the raw coordinates of the point. I have to paste the URL somewhere and select the coordinates.

I’d like to write an AppleScript to extract the coordinates of the pin, but the AppleScript library for Maps is, for all practical purposes, empty, and UI scripting is never going to work with all the clicking that’s necessary.

Keyboard Maestro to the rescue. I put together a macro that does all of the above steps and pulls out just the pin coordinates from the URL. It solves the problem of where to click by allowing you to specify an image to aim at. Here, for example, is the step for clicking on the pin to show its flag:

By restricting the target image to just the inside portion of the pin, this works in Standard, Satellite, and Hybrid view—the clutter around the pin doesn’t screw up Keyboard Maestro’s “aim.”

The extraction of the coordinates from the URL is done through a simple shell pipeline at the end of the macro:

The pipeline is

pbpaste | sed -E 's/^.+\?q=([0-9.,-]+).+/\1/'


which grabs the two comma-separated numbers after the q= and puts them on the clipboard. Passing the -E option to sed makes its regular expression syntax more like the Perl/Python syntax I’m used to. If it weren’t for that option, I’d find sed completely unusable.

If you’re interested in this macro, I put a zip file of it on my server. A few of the click actions are on popups that don’t appear instantly, so there are pauses at certain steps to insure the popups’ presence. You may need to adjust the lengths of these pauses to make the macro run smoothly on your system.