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.

Related posts

Tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Subscribe without commenting