#!/usr/bin/env python """latexcount.py: program to count the number real words in a .tex file. SYNTAX: latexcount.py FILENAME""" import sys, string if len(sys.argv) < 2: print __doc__ sys.exit(0) filename = sys.argv[1] f = open(filename,'r') lines = f.readlines() f.close() wc = 0 ignore = 0 brace = 0 math = 0 for line in lines: if line[0] != '%': words = string.split(line) for word in words: if word[0:6] == "\\begin": ignore = 1 if word[0] == '$': math = 1 if ignore == 0 and brace == 0 and math == 0: if word[0:2] != "{\\" and word != "--": if word[0] != "\\": wc = wc + 1 else: if string.count(word,"{") != 0: brace = 1 if brace == 1: if string.count(word,"}") != 0: brace = 0 if word[0:4] == "\\end": ignore = 0 if string.count(word,"$", 1) != 0: math = 0 print wc