Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Often assignments (or even papers) have a word count limit. That is not a big deal when using Word, but I don't know how to do that using LaTeX. My solution has been so far to compile the document and then do a rough word count of my pdf file, sometimes even copying the contents of the pdf file and pasting in Word to get a mostly correct Word count.

Is there any tool (maybe even an online tool), package, script or software to do that directly from my .tex document and still get the right word count (i.e., ignore commands, equations, etc)?

share|improve this question
    
Same question on Stack Exchange: stackoverflow.com/questions/2974954/… –  Charles Stewart Jun 18 '11 at 12:07
10  
Under Linux I normally do it over the PDF to get a rough count: pdftotext file.pdf - | wc -w, but this also counts page numbers etc. as words. –  Martin Scharrer Jun 29 '11 at 18:27
5  
Word count is never perfectly defined: How much words in can't? In an algorithm? In a figure with several texts? So, the notion of correct word count does not exist... –  Paul Gaborit Jul 17 '12 at 11:43
    
Emacs native tex-mode has a word count function: M-x tex-count-words. –  giordano Jun 4 '13 at 8:33
2  
watches your LaTeX word limit as you type and save: % watch -d "detex index.tex | wc -w" –  Vaibhav Bajpai Feb 28 at 19:43

17 Answers 17

up vote 84 down vote accepted

This is in the UK TeX FAQ. The solutions suggested are:

  • detex filename (which tries to strip LaTeX commands), then use any word count tool. (E.g. wc)

  • latexcount.pl, a Perl script for word count

  • texcount, another script even has an online interface

  • wordcount, which has a script that runs LaTeX with some settings, then counts word indications in the log file.

share|improve this answer
6  
texcount is pretty neat, especially the friendly online interface. One caveat: it recognizes align, equation, [ ], and $ as defining math environments (and possibly more). But it somehow misses align*. –  Willie Wong Jul 29 '10 at 1:01
10  
Just want to address my previous comment: the author got back to me and it will be fixed in the next version (2.3) –  Willie Wong Jul 29 '10 at 11:19
1  
Do these methods ignore all the words inside math environments? –  Malabarba Sep 28 '10 at 16:33
1  
If you use texcount you'll want to run it with options like -inc -incbib -sum to get a more accurate total. –  Seamus Jul 17 '12 at 9:56
    
another caveat with texcount: it works off the raw tex file, so it will ignore any inline references placed using bibtex. this makes it worthless for most academic articles. –  Jeff Nov 28 '12 at 1:27

Here’s an excerpt from my .vimrc that gives me a comfortable word count in Vim:

function! WC()
    let filename = expand("%")
    let cmd = "detex " . filename . " | wc -w | tr -d [:space:]"
    let result = system(cmd)
    echo result . " words"
endfunction

command WC call WC()

Now I can invoke :WC in command mode to have the word count echoed in the status line.

share|improve this answer
4  
I cannot help but think that calling perl just to do chomp and a regex is way too much of an overkill. =) Why not just ' ... | wc -w | tr -d [:space:]'? –  Willie Wong Aug 4 '10 at 0:06
    
@Willie: good call. Don’t remember why I used Perl here. –  Konrad Rudolph Aug 4 '10 at 8:38
6  
now I would be much happier if you had given me something I can use in Emacs! :P –  Vivi Aug 4 '10 at 9:45
1  
@Vivi superuser.com/questions/125027/… –  Seamus Feb 15 '12 at 12:05
32  
@Vivi: odd name for an Emacs user! :P –  naught101 Apr 24 '12 at 4:29

Way back in the depths of time, I scribbled my own perl script to do this. My reason for doing this myself was that sometimes I wanted to count words in command arguments and sometimes not, so I built in a selection routine. Plus I figured that a bit of maths was worth a word so added that in. As the script is really simple, I'm copying it here (which automatically makes it some sort of free-to-use, I guess!).

I don't think that I've used it for years, though - it's been a long time since "number of words" mattered to me at all.

#!/usr/bin/perl -w

@ARGV and $ARGV[0] =~ /^-+h(elp)?$/ && die "Usage:\t$0 files\n\t$0 < files\n\t$0\n";

my $count = 0;
my $first = "";
my $tex = 0;

while ($first =~ /^\s*$/) {
    $first = <>;
}

if ($first =~ /^\\(input|section|setlength|documentstyle|chapter|documentclass|relax|contentsline|indexentry|begin|glossaryentry)/) {
    $tex = sub { $r = $_[0];
                 $m = $_[1];
                 $r =~ s/\\(emph|textbf|textit|texttt|em)\{//g;
                 $r =~ s/\\(sub)*section\*?\{[^\}]*\}//;
                 $r =~ s/\\title\{[^\}]*\}//;
                 $r =~ s/\\\(.*?\\\)/maths/g;
                 $r =~ s/\\\(.*?$/maths/;
                 $r =~ s/^.*?\\\)/maths/;
                 $r =~ s/\\\[.*?\\\]/maths/g;
                 $r =~ s/.*?\\\]// and $m = 0;
                 $m and $r = "";
                 $r =~ s/\\\[.*?$// and $m = 1;
                 $r =~ s/\\\S*//g;
                 $r =~ s/%.*//;
                 return ($r,$m) };
} else {
    $tex = sub { return ($_[0],0) };
    @split = split(" ", $first);
    $count += $#split + 1;
}

while ($s = <>) {
    ($t,$n) = &$tex($s,$n);
    @split = split(" ", $t);
    $count += $#split + 1;
}

print "Number of words: $count\n";
share|improve this answer
4  
@Andrew: again the issue: what do you do with it? how I am supposed to use it? Can you add that to your answer? –  Vivi Jul 29 '10 at 8:44
4  
@Viv: to be honest, I would say that if you don't know what to do with this then you aren't supposed to use it and should use one of the answers in the accepted answer. –  Loop Space Jul 29 '10 at 9:18
12  
I am glad you were born knowing how to do it, otherwise you wouldn't be using it now, right? Because if you don't know it, you are not supposed to learn it, isn't that right? –  Vivi Jul 29 '10 at 10:59
6  
@Vivi: I apologise for the fact that my remark has come across other than I intended it. I did start writing out the instructions, but they are so OS and user specific that I gave up. I'd be happy to help with this sort of thing (though I think that the comments here are not the best format for such help), but in this specific case I really do think that if you don't know how to do it, then this is not the right way to do it. It's an old script that I dug up and it is very me-specific so almost certainly would need a little tweaking to make it useful to anyone. (contd) –  Loop Space Jul 29 '10 at 11:09
7  
@Andrew: In Brazil I am Vivi, but here I am Viv, so you didn't spell it wrong after all! Thanks for your answer. I understand some things are too complicated to explain here, and it is not really the place. This thing (scripts, and pearl) keeps coming up so often that I decided it is time to understand it! I will ask this to someone that lives here and can explain to me in person (about using scripts in general, but I will probably leave yours for later, given what you said). Thanks again for taking the time to explain, and thanks for sharing the code :) –  Vivi Jul 29 '10 at 11:58

The first one to come to mind is detex which strips a tex file of commands. You will then have to pass it through wc or some other word counting software. A search on the internet also brought up two items on Sourceforge: word counter 1 and word counter 2.

Disclaimer: out of the three, I've only used detex before. It worked reasonably well, but I was working with an English essay and it had no equations, so I don't know how it plays with math mode stuff. (Currently I don't have it installed so I can't check.)

share|improve this answer
    
It's been a while since I used it but if I remember correctly, I don't think detex completely strips out the math content of a file, so that might skew the word count. –  David Z Jul 29 '10 at 0:47
2  
I had opportunity to use detex recently and it leaves many TeX-related words which have nothing to do with the content. I would almost go as far as saying that compiling to PDF and then using pdftotext might produce a more accurate count, even when it contains page numbers and repeats the headers. –  José Figueroa-O'Farrill Jul 29 '10 at 0:50
2  
Just tried it on a recent paper I wrote: yeah, detex is wildly off. I get even better results from piping dvi2tty to wc. –  Willie Wong Aug 4 '10 at 0:51
    
Never used detex, but untex which comes in Debian seems to do the job. –  helcim Aug 6 '10 at 15:23
    
@José: I found pdftotext to be much better. Especially since I tend to write macros that generate text so stripping them produces wildly incorrect word counts. –  TH. Nov 27 '10 at 17:41

Compile the Tex-File to DVI and then execute

 catdvi document.dvi | wc -w

This converts your DVI file to a text-only file and counts the words using 'wc'.

share|improve this answer
    
This gets fooled by hyphenation but should be a good approximation. –  lhf Jul 17 '12 at 10:46

The Texmaker integrated pdf viewer offers a word count feature since version 3.4.
Just right-click in the pdf document, then click Number of words in the document.

enter image description here

share|improve this answer
    
Wow, I’ve been using that editor for a while but didn’t know this! I just played around with it for a little bit and it seems that the word count includes everything that has a space between itself and the next thing, i.e. the page numbers, the section numbers and titles, the section numbers and titles in the ToC, the ToC title, and even every dot in the dotted line for the \subsections in the ToC. –  doncherry Apr 7 '13 at 17:08

You can use the word count code from Context (lang-wrd.lua). I took the liberty and adapted it for Plain (should work with the LaTeX format as well). The code is stripped of more Context specific features and relies on the character property definitions from char-def.lua. This way there’s no need for external tools and as a bonus you can insert the current word count wherever you like inside the document itself.

The usage example has some explanations.

\setwordthreshold{3} %%% min chars in a row to count as word
\startwordcount      %%% start callback
\input knuth\par     %%% counted
\currentwordcount    %%% => 94 with threshold == 3
\input knuth         %%% counted
\stopwordcount       %%% deregister callback
\input knuth         %%% not counted
\dumpwordcount       %%% => 188

Everything between \startwordcount and \stopwordcount picked up, the rest will be ignored, so you can manually exempt passages from being counted. The word threshold would have to be set to 1 for English.

Due to the nature of thre pre_linebreak_filter you will get word counts only by paragraph, though.

share|improve this answer
    
This looks promising.. Is it possible to dump the total wordcount before starting to count? –  Frederik Nielsen Aug 21 at 7:23
    
You’d have to store it somewhere between TeX runs. Write the result of packagedata.word_count.current_word_count() to some temporary file and re-read it at the start of the next run. –  phg Aug 21 at 16:25

For Windows users, the LaTeX Word Counter is pretty neat.

share|improve this answer
    
The Sourceforge page says it's also available for Linux and Mac. –  doncherry Feb 15 '12 at 10:41

The last time I had to worry about this, I compiled my LaTeX document to PDF and ran it through pdftotext.

share|improve this answer
    
This approach cannot deliver reliable results due to unreliable conversion by pdftotext. –  helcim Aug 6 '10 at 15:26
    
Do you have an example of how pdftotext fails? I got reasonable results when I used it, but the documents I was using were not particularly elaborate (I don't think they had figures, for example). –  Blake Stacey Aug 8 '10 at 18:46
1  
This will count headers and page numbers in your word count. This will also count lots of words in mathmode. Try doing pdftotext and then wc on a file containing the equation $x+y=z$. That counts as something like 5 words for this method… –  Seamus Jul 17 '12 at 9:55
    
@Seamus For documents without much logic or maths, though, I've found it more accurate than alternatives when combined with a script to remove headers, footers etc. For documents with a lot of logic or maths, it would be hopeless. (I have no idea how those should be calculated, either.) –  cfr Sep 12 at 23:45

If you are on Windows and do not mind purchasing software, use WinEdt. It has a built in word count feature (Document->word count).

share|improve this answer

You can obtain texcount results in the own LaTeX document:

MWE

Note that this MWE require the filename borra.tex (or modify the code accordingly).

% CAUTION !!!
% 1) Need --enable-write18 or --shell-escape 
% 2) This file MUST be saved 
%    as "borra.tex" before the compilation
%    in your working directory
% 3) This code will write wordcount.tex
%    and charcount.tex in /tmp of your disk.
%    (Windows users must change this path)
% 4) Do not compile if you are unsure
%    of what you are doing.

\documentclass{article}
\usepackage{moreverb} % for verbatim ouput

% Count of words

\immediate\write18{texcount -inc -incbib 
-sum borra.tex > /tmp/wordcount.tex}
\newcommand\wordcount{
\verbatiminput{/tmp/wordcount.tex}}

% Count of characters

\immediate\write18{texcount -char -freq
 borra.tex > /tmp/charcount.tex}
\newcommand\charcount{
\verbatiminput{/tmp/charcount.tex}}


\begin{document}


\section{Section: text example with a float}

Words and characters of this example file are 
automatically counted from the source file 
when compiled (therefore generated text as 
\textbackslash{}lipsum[1-10] is {\bfseries not} 
counted). The results are showed at the end 
of the compiled version.
Counts are made in headers, caption floats 
and normal text for the whole file. Subcounts 
for structured parts (sections, subsections, 
etc.) are also made. Number of headers, 
floats and math chunks are also counted. 

\begin{figure}[h]
\centering
\framebox{This is only a example float} 
\caption{This is a example caption}
\end{figure}

\subsection{Subsection: Little text with math chunks}

In line math: $\pi +2 = 2+\pi$ \\   
Display math: \[\pi +2 = 2+\pi\] 

%TC:ignore  
\dotfill End of the example \dotfill 

\subsubsection*{Counts of words} 
\wordcount

%TC:endignore   

\end{document}
share|improve this answer
    
I suggest you change it to:\immediate\write18{texcount -nc -inc -sum \jobname.tex > wordcount.tex} \newcommand\wordcount{\verbatiminput{wordcount.tex}} % Count of characters \immediate\write18{texcount -char -nc -freq \jobname.tex > charcount.tex} \newcommand\charcount{\verbatiminput{charcount.tex}} then becomes filename independant. –  Louis Apr 5 at 16:31

In general the answer is NO.

Nearly all requesters of word counts are not interested in the number of words but rather in the amount of space (pages) that the document will need when printed. If there are figures should the words in captions be counted without the space required by the illustration being taken into account? Are equations words, and if so is it one 'word' per variable/symbol or one 'word' per equation? If a paper consists of nothing more than title, author, a sentence and 100 math expressions is that about 50 or 500 'words'? Is a hyphenated word one or two? Does a document that mainly consists of 3 or 4 letter words compare equally with one that has a preponderance of 8 to 10 letter words?

I think that the traditional method is best: print the document, count the average number of 'words' per line in a typical page and multiply by the average number of lines per page and by the number of pages.

It is highly unlikely that the recipient of your work will actually count the number of words.

share|improve this answer
6  
:@peterwilson: i don't think that's the case; universities are notorious for setting word counts for dissertations, and then employing a menial to count the words when the copy is submitted. in this case, they don't (really) care about the printed size, they care about the idiot letter of their idiot regulations. (iirc, my university (cambridge) has recently followed down that crazy path, having previously set a page limit.) –  wasteofspace Jul 17 '12 at 10:40

You can try Microspell. It's a very robust software that knows if you have a main tex document and other subsidiary ones.

share|improve this answer

kile the latex editor for the kde (ubuntu) desktop has a word count. It is under the statistics menu

share|improve this answer
1  
To have that clear, kile has pretty much nothing to do with ubuntu, or its desktop. –  mafp Jan 8 '13 at 0:42
    
It is not at all accurate. It is OK to get a rough sense of whether document 1 has more words than document 2 but you could not use it for anything requiring an actual count, even an approximate one. –  cfr Sep 12 at 23:39

I use texcount with the following parameters:

texcount file.tex -inc -incbib -sum -1

Output is simple like this:

9079

If you remove the -1, then you can get more information:

word count (#headers/#floats/#inlines/#displayed)
3996+48+99 (22/9/0/0) Included file: parts/blup.tex
share|improve this answer

Better method. Copy text into MS Word. Let Word do the word count.

share|improve this answer
1  
This would most certainly include headers & footers. –  Werner Feb 27 '13 at 7:14
3  
The answer could be improved by replacing MS Word by word processor programs in general, if the user isn't bound to Microsoft. –  Stefan Kottwitz Feb 27 '13 at 11:59

For Mac users, TeXShop (at least version 3.26) has a line, word and character count under Edit>Statistics. I never tested how well it works, but since TeXShop recognises syntax for colour-coding, I assume it is able to ignore most commands for the text.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.