Setting up word count for LaTeX

Occasionally it can be useful to have a word count for a document I am writing. Being a nerd who hates word processors and tends to use a lot of maths in most things I write, I use LaTeX.

So how can you get a reliable word count in LaTeX? There is a brilliant program called TeXcount which word counts a LaTeX file. It has many options and can output to the terminal or give a detailed summary in HTML. TeXcount is included with many LaTeX distributions, or can be downloaded from the link above. No need to install, just unzip the files. But getting it to run nicely can take a couple of tricks.

Perl

First, TeXcount is written in perl so you will need perl to run it. If you are on a unix system you should have perl already. On windows you will need to install perl. To install oerl simply run the strawberry perl installer.

Options for a nice display

TeXcount has a great number of options. You can explore these by reading the PDF the comes with TeXcount. For me I run with the three following options:

  • -html     – This makes the output HTML code which when displayed in a browser give a nice overview of the number of words, words in headings words in caption, equations etc.
  • -inc    – This tells it to also word count any extra .tex files included in the .tex file you ran.
  • -incbib  – This tells TeXcount to also word count the bibliography, you may or may not want this.

Actually running TeXcount

Of course you can just run TeXcount from the terminal with

texcount.pl -html -inc -incbib myfile.html

or if texcount is not in the PATH

path/to/texcount/texcount.pl -html -inc -incbib myfile.html

(using \ not / if you are in Windows). This is not to convenient because it the puts all the HTML into the terminal. You can of course use > to redirect the output into a file. But opening the terminal and finding the directory and writing (or reverse searching) the commands is tedious. Instead one can write a simple 3-4 line script to run TeXcount and displays the output in a browser.

UNIX – save this code as “RunTexcount.sh” in the same folder as TeXcount (for KDE you can change “gnome-open” to “kde-open”, or mac simply “open”):

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
"$DIR"/texcount.pl -html -inc -incbib "$1".tex > "$1"_count.html
gnome-open "$1"_count.html

Windows- save this code as “RunTexcount.bat” in the same folder as TeXcount:

@echo off
%~dp0\texcount.pl -html -inc -incbib "%1".tex > "%1"_count.html
"%1"_count.html

Now any LaTeX IDE worth using has a way of setting up custom executables. (In Kile go to Settings>Configure Kile>Build>New, in WinShell go to Options>User programs.)
Here you can simply set the .sh or .bat file as the executable. For the options (cmd line) you need the name of the .tex file WITHOUT the file extension. This can usually be done using '%s' or "%s". The easiest thing to do is look to see how BibTeX is set to run and use the same commands.

Now, by simply using an option in your LaTeX IDE menu TeXcount runs and the output is opened in your browser. I normally set this to a keyboard shortcut, so I don’t even need to use a menu.

Simple!

2 thoughts on “Setting up word count for LaTeX

Leave a Reply

Your email address will not be published. Required fields are marked *