Blog

Counting words in LaTeX documents with TextMate

When working on LaTeX documents in TextMate, the default word count feature isn’t too helpful, as it also includes all of the LaTeX control sequences in the count. There are plenty of suggestions out there, but this is one I came up with, which works the same way as the default word count facility (Ctrl-Shift-N):

#!/usr/bin/env ruby -wKU
 
def pretty(number)
  number.to_s.gsub(/\d{1,3}(?=\d{3}+(?!\d))/, '\0,')
end
 
counts = `detex -l | wc -lwc`.scan(/\d+/)
counts[0] = counts[0].to_i + 1 # increase one to the line count
 
%w[ line word byte ].each do |unit|
  cnt    = counts.shift
  plural = cnt.to_i != 1 ? 's' : ''
  printf("%11.11s %s%s\n", pretty(cnt), unit, plural)
end

This is just a copy of the default statistics action from the Text bundle, but with the ‘detex’ utility added into the pipeline to remove all LaTeX control sequences.

Put this in a new command in the LaTeX bundle, with ‘Selected Text’ for input, and ‘Show as Tool Tip’ for output. I assigned Ctrl-Shift-N as the key equivalent.

. Bookmark the permalink. Both comments and trackbacks are currently closed.

One Comment

  1. Jono
    Posted October 28, 2011 at 11:53 am | Permalink

    Amazing – thanks! Worked perfectly :) A much better solution than ps2ascii. Perhaps you might want to create a bundle for easier installation?

    J