Word count in XeLatex

Posted on 21 August 2013

A while ago I switched to XeLatex from Latex as it can do some amazing things. One particularly cool thing is word counting from within the document itself. This is great for paper abstracts! I got the code from here (or possibly somewhere else with similar code – it was a while ago!) and tweaked it a bit to produce my wordcounter environment:


%% word counting environment
\expandafter\ifx\csname wordcount@xs@searchlist\endcsname\relax

   {word count: \arabic{words} words}%
\begingroup\expandafter\def\csname @currenvir\endcsname{wordcounter}%

Usage is very simple – just wrap the part of the document that needs to be word-counted in the wordcounter environment, and a message will be inserted into the document after the text giving the word count. For example

    blah blah blah blah blah