Archive for March, 2013

Add hidden, searchable text-layer to PDF via OCR with Adobe Acrobat

Friday, March 29th, 2013

Forgot that I knew how to do this.

Document > OCR Text Recognition > Recognize Text Using OCR …

Sparse matrix equality in MATLAB accidentally dense

Thursday, March 28th, 2013

I had in my code:

all(A(:) == B(:))

where A and B where both large, sparse matrices. Unfortunately the == operator in MATLAB is not conducted in a sparse way. Try this:

A = sparse(n,n);
B= sparse(n,n);

There are zero non-zeros so this should cost no time at all. But the all() function is of course not smart enough to unravel the == operator. So this is the same as doing:

C = A(:) == B(:);

And of course all the zeros that match will turn into 1s in C, so if A and B are the same C will be completely dense.

Instead I should use the built-in


Note that in the case where is A mostly the same as B then


will be fast. But if they’re different, this is effectively dense as above.

Color citations with textcolor in LaTeX

Wednesday, March 27th, 2013

I’m trying to highlight changes in a LaTeX document. I tried to do this using \textcolor. So my LaTeX looks something like:

This is the \textcolor{red}{oldest citation possible \cite{Kish:3500BC}.}

Which produced something like:

This is the oldest citation possible [Kish 3500BC].

The problem was that I was using a class (*.cls file) that was coloring all hyperlinks, including citations, black. To turn this off I changed:

\hypersetup{colorlinks=true, ...


\hypersetup{colorlinks=false, ...

Now my coloring comes out correctly:

This is the oldest citation possible [Kish 3500BC].

Safari 6 (almost) hangs when adding new bookmarks

Tuesday, March 26th, 2013

I upgraded to Safari 6 and was upset that when I added a new bookmark I got the beach ball of death (is there also a beach ball of life). I impatiently killed Safari after 10 secs or so.
I tried adding bookmarks in any way I could, but always it seemed to hang.
Finally I resolved to give up but left Safari spinning. When I came back the bookmark was added. Not sure what it was doing or thinking about (perhaps whether there is also a beach ball of life), but now I can add bookmarks again without any delay.

Switch search to google scholar bookmarklet

Monday, March 25th, 2013

I upgraded to Safari 6 and was happy to see the search bar gone and keyword search directly in the address bar. However I was annoyed to see that when searching you don’t see the full google url but just the query keywords. This nullified my previous hack to switch a google search to a google scholar search.

Instead here’s a little bookmarklet to switch a google search to a google scholar search:

javascript:(function(){window.location.href=window.location.href.replace("search?", "scholar?");})()

Just paste that into you address bar and create a bookmark for it. Then any time you do a normal google search, you can switch it to a google scholar search by hitting the bookmark.


Download web file onto a server from client

Tuesday, March 19th, 2013

If I want to download a file from a URL I can use wget. Then I can use scp to copy to my webserver. Or I could ssh to the server and call wget from there. Here’s a one-liner to do just that:


Preview crashes when reopening PDFs created with LaTeX

Sunday, March 17th, 2013

To typeset documents I use pdflatex. I edit my tex files using vim and build the pdfs using a makefile. At the end of my make sequence I like to have:

open my.pdf

Which on my mac causes preview to open my pdf document.

When I recompile and this command runs, sometimes preview just focuses the already open document, but other times it opens a new window. I haven’t backwards engineered how it decides which to do, but it might be correlated with the amount of change in between. In any case, Preview recently has been crashing when it tries to open the same document in a new window. For small, short-term articles this is not so annoying. But now, writing my t****s, I’m really getting frustrated.

I wrote an applescript to replace the open command above. First you’ll need to enable applescript for Then you can save the following in preview.scpt.

on run argv
	set file_name to item 1 of argv
		tell application "Preview"
			set open_already to false
			repeat with this_doc in every document
				set open_already to open_already or (name of this_doc as string is equal to file_name)
				if open_already then
					return "open already"
				end if
			end repeat
			if not open_already then
				open (do shell script "pwd") & "/" & file_name
				return ""
			end if
		end tell
	on error errMsg
		return errMsg
	end try
end run

You can run with:

osascript preview.scpt my.pdf

The rerun to see that no action is taken.

the name “🍌” can’t be used

Saturday, March 9th, 2013

the name 🍌 can't be used

skatepark as richard serra sculpture

Wednesday, March 6th, 2013

Skatepark as richard serra sculpture

Close figure 1 in matlab only if it exists

Tuesday, March 5th, 2013

It was annoyingly difficult to find an elegant way to close a certain figure without causing an error if that figure doesn’t exist. Using gcf or figure(1) are bad ideas because if the figure doesn’t exist then it’s created. So calling something like:


when figure 1 doesn’t exist causes it to open then close. I came up with this instead: