Penguin

Option 1 - detex

If you try to get a wordcount of a latex source file via the shell command 'wc', it will include any macros you have used, and any comments, in the word count.

A better way is to make use of a tool called detex. This does a (fairly) good job of stripping out any Latex macros and comments, although it's probably not usable as a .tex -> readable ascii filter (it gets the ordering on included files wrong, for starters). What it does do is automatically include referenced Latex files (at least the ones using \input ), so you don't have to add any numbers.

I added the following rules to my Makefile:

.PHONY: wordcount

wordcount: thesis.tex
       detex thesis.tex | wc

running 'make wordcount' will now run detex over my .tex file, and pipe the output through the 'wc' program:

If I do a 'normal' wordcount:

$ cat thesis.tex | wc
   1419    7738   57862

Compare this to our new make target:

$ make wordcount
/home/dlawson/bin/detex thesis.tex | wc
   1009    5528   35563

A noticeable difference!

detex isn't 100% accurate though. It doesn't seem to understand conditionals ( \ifx1 do this \else do that \fi) or arguments that aren't part of the content (like \usepackage{hyperref}).


Option 2 - pdftotext/ps2ascii

Alternatively, convert the .tex file to ascii and run through wc:

$ pdflatex report.tex
$ ps2ascii report.pdf | wc -w
   2003

Note that because postscript/pdf is a page description language, this conversion is not completely accurate as it has to heuristically guess where word breaks are. I noticed that this broke some words into two, especially for larger fonts like section headings. Some letter combinations seem to be worse than others... "project" and "object" were consistently broken into "pro ject" and "ob ject". Don't treat the number of words as gospel :)


Option 3 - dvi2tty

As you can guess from the name, this is designed for rendering a .dvi file to a terminal (or to a file). The disadvantage of this is that you have to be using plain latex instead of pdflatex, so that you can generate a .dvi file. IMHO this is a better approach than the previous two because LaTeX has already parsed the source files so it doesn't need to worry about tex/latex commands, but the format still has enough information to keep words intact (except where TeX has hyphenated words at the margin).

# -w100 means format for 100 chars wide
# perl to remove punctuation so wc doesn't count them
# and remove hyphenation at the end of lines
dvi2tty -w100 /path/to/file.dvi \
 | perl -pe 's/-$// || s/$/ /; chomp; s/[-\._|]//g' | wc -w

You want the dvi2tty package in Debian Sarge, dev-tex/dvi2tty in Gentoo, or get it from ftp://ftp.mesa.nl/pub/dvi2tty/dvi2tty-5.3.1.tar.gz


Option 4 - copy paste

Copy you document from a pdf, dvi or a postscript viewer and paste into word. Do the usual word count.

This will over-estimate in most cases.