The number of words in a LaTeX document can be calculated using the TeXcount utility, which is available on CTAN and is part of the TeX Live distribution. This post demonstrates how to automatically include the word count in the PDF document during compilation.

Consider the following LaTeX file, with the file path doc/main.tex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
\documentclass{article}

\title{Document title}

\begin{document}

\maketitle

\section{Section title}

Sample text. Sample text. Sample text. Sample text.

%TC:ignore
% insert word count at the bottom right
\vfill\hfill Word count: \input{wordcount}%
%TC:endignore

\end{document}

Run the following command to switch to the file’s directory, get the word count, write it to a file with the path doc/wordcount.tex, and compile the PDF:

cd doc && texcount -merge -template='{1}' main.tex -out='wordcount.tex' && xelatex main.tex

In the texcount command, -merge ensures that all included files are merged into the document during the count, and -template='{1}' uses the output template that counts only the main body text. See TeXcount’s website or CTAN documentation for more information about the command-line options available.

This is the resulting document:

PDF output.
PDF output.

All text enclosed within %TC:ignore and %TC:endignore will be omitted from the word count1.

To add thousands separators2 3 to the word count, use the following command instead:

cd doc && printf "%'d" $(texcount -merge -template='{1}' main.tex) > wordcount.tex && xelatex main.tex

Footnotes