Wednesday, 22 September 2010

Four gnuplot Tricks

Depending on what you do on a day-to-day basis, you might use the plotting program gnuplot. If you film dolphins underwater, probably not. But if, like me, you have to digest pages and pages of columned simulation data, maybe you do, and in that case I have a few tips and tricks you might not know about.

I'm going to presume that you're reading this as someone quite familiar with gnuplot. It might not be pretty but it's quick and shows me what I need to see. (This sentence could be removed from context with disastrous consequences...) I don't use it for producing plots for publication or presentation but if I just need a quick look at what's going wrong with my PhD, gnuplot fits the bill. With a few abbreviations to speed up the process, it's actually really useful.

All of these tips are from my daily routine which takes place in Bash terminals, so I can't guarantee they'll work on every system. In particular, I'm not sure about how much will work with Windows. But why are you trying to do work in Windows?

Plot Window Shortcuts

There are a couple of commands you can issue while the plot window is selected. For example, pressing "e" issues a replot command. For a complete list, select a plot window and press "h". The list will appear in the command window.

Terminal Abbreviations

This is a really simple trick that I'm including just in case you missed out on it. One can abbreviate just about every terminal command in gnuplot. For example,

plot "file" using 1:2

can be shortened to

p "file" u 1:2

If you're used to hacking away at the terminal, this is a must. What's more, this isn't from a standard list: as long as the abbreviation is unambiguous in context, it should work. So p above could just as well be pl.

Using Shell Commands in the Filename

What if you only want to plot from the last 200 lines of the file? There are some native gnuplot instructions that will do this but an alternative way, for the example above, is

p "< tail -200 file" u 1:2

See what I did there? You can effectively construct a new output stream with whatever terminal command you'd like. I personally have a formidable armoury of sed, awk and egrep commands that I use to work with my data. Okay, formidable for the PhDemon rather than the US Marine Corps, but I don't have to wrestle with the US Marines Corps on a daily basis.

Command History Search

A familiar trick for Bash users is the ability to search your history for a previously issued command. Well, if you didn't know about that, now you do. If you don't feel like pressing the up arrow 206 times, press "Ctrl+R" to get a mini-prompt which you can use to search all your previous commands. I suspect this is an inherited Bash feature, so I'm not sure if it works in all circumstances but it definitely works in GNOME.

Tuesday, 7 September 2010

My reasons for using RSS and Google Reader

Taking advice from Matt Might, I took another step into the 21st century a short while ago by starting to use Google Reader to aggregate RSS feeds. For the sake of productivity, if you don't use RSS feeds already, you really should. Here are my reasons why, along with how I'm doing it. (If you don't know what RSS is, have a look at the opening paragraph of the Wikipedia article.)


By subscribing to an RSS feed, you're guaranteed to miss nothing. For webcomics, that means I save time because I don't have to open a new tab. It's on a page I'm already looking at. For news sites, it means I don't look at a frontpage of stories I've already seen.

For academic journals, it means I don't end up double-checking titles or abstracts I've already seen. I find this particularly useful because the journals I look at aren't very good about letting you know what's new since the last visit, even though they have facilities for displaying accepted articles before they appear in the printed copy.

For everything, it means that if I'm away for a while, I don't have to go back through the logs trying to work out where I left off. It's all waiting for me in Reader and I know whether I've seen it or not. Okay, granted that might be many hundreds of posts, but at least I won't miss a paper that's scoops my PhD project...

As time goes on, you'll find it quick to sift through your feeds. But beware the temptation to gorge yourself like some kind of infovore by subscribing to every technology news site on the Internet.

Google Reader

Like all Google's web tools, I use Reader because it's accessible from anywhere. In my case, this means I recover some dead time at home. I mostly restrict working to the workplace. After all, I have better odds against the PhDemon when I have 4 cores, 4GB of RAM and a 24" screen. But with the RSS feeds for journals coming through, I can look at the feeds and clean out stuff I don't want to read even on my six-year-old P4 hand-me-down. Conversely, Flash isn't installed on the computers we use in the office, so I leave embedded videos for home viewing. Basically, I look at materials where it's most convenient.

A lesser point is that I've found Reader's recommendations pretty good. It's how I picked up on a blog like AstroBetter. It's also meme-tastic. My recommendations always seem to be right on top of dominant virals and popular websites. The Wilderness Downtown, Arcade Fire and Google's HTML5 demo, popped up on a recommendation shortly before appearing in every tech blog across the Internet. (Another reason for not being an infovore is the redundancy. I'm pretty sure all the science sites base their stories on the same limited set of press releases.)

Custom Feeds

All this said, some RSS feeds aren't perfect. I'm a reader of the webcomic Ctrl+Alt+Del. The RSS feed on the site doesn't seem to display the comic... which is kind of the point of a feed for a comic. That means I have to click to get it. Fortunately, someone else who knows more than me has made a fix.

Moreover, some feeds that you might want don't seem to exist. Don't despair though: if you search around a bit, you may well find that again, someone else has fixed the problem. I looked hard for a Zapiro feed on the sites of the newspapers that print his cartoons but instead I found it here.

So if there's something you read online, find a feed. Seek the orange button. Remember an unofficial one might be more efficient or exist where there's no official alternative at all. With a good aggregator, like Google Reader, you'll save yourself a lot of time, which either means you can spend more time elsewhere or just read more on the web. Instead of going out looking for stuff on the web, you can make sure it's sent to you.

Friday, 3 September 2010

Finding symbols in LaTeX

There are a lot of symbols in LaTeX and finding the right one, especially when first learning, can be a bit of a pain. It happens very infrequently now that I've been using LaTeX for more than four years, but when I need to find some symbol, I look to two resources.

Cheat Sheets and Reference Cards

A few people have put together one or two page tables of the most commonly used constructs. They're generally referred to as "cheat sheets", "reference cards" or something like that. Googling them will give you plenty of hits. The two that have sat on my desk since I started using LaTeX are Winston Chang's LaTeX 2e Cheat Sheet and J. H. Silverman's TeX Reference Card. Print out the PDFs and just stick them somewhere nearby and accessible.


DeTeXify is an amazing HTML5 tool by Philipp Kühl and Daniel Kirsch. Just try to draw the symbol you want and the script will give you its best guesses. If you're a seasoned LaTeX user, I'd recommend visiting it and trying your most obscure symbols so you can teach it. This tool should be spread as far and wide across the Internet as possible.

Note that because the applet uses HTML5, you'll need a recent browser. The page itself claims that Opera 9.6+, Firefox 3.5+ and Safari 4+ work. Google Chrome will almost certainly work too.

Wednesday, 1 September 2010

Joining and including PDFs in LaTeX

Have you ever wanted to join a set of separated PDFs together? Maybe a book broken into chapters or sections? There's a useful package that makes this easy with LaTeX: pdfpages. As far as I can tell, it's included in most LaTeX distributions. You can find the complete documentation at the CTAN entry but basic use is simple.

Basic Use

Include the package by placing the line


in your document's preamble i.e. before \begin{document}. Then, where you want a PDF to appear, put the line


where file.pdf is the relevant file. If you only want to include pages x through y, use


instead. If you omit x and y completely it includes the entire document. Note that when you compile, you have to compile straight to PDF: as far as I can tell, this won't work if you compile to DVI or PS first.

The main use I have for this is to join files that represent parts of a single document back together. I come across such situations often in my searches for lecture notes and other free materials. I now just a have a single LaTeX file with a string of includepdf's that I adjust each time, compile and then rename the output.


As a part-worked example, here's how I joined the obsolete Numerical Recipes in Fortran 77, Second Edition, into a single (monstrous) PDF. The second edition of NR is available free for C, Fortran 77 and Fortran 90. The PDFs are separated into sections: each file in the F77 version is labeled fc-s.pdf, where c is the chapter number and s is the section number. Beyond the introductory materials, many sections don't start on new pages so some pages are doubled up. I included each file with pdfpages and specified the page range to skip the first pages of sections that don't start on new pages. The result is a LaTeX file that looks like this:





I compile it with pdflatex and now have NR F77 in a single file for reference.