79

I'm currently searching for an application or a script that does a correct word count for a LaTeX document.

Up till now, I have only encountered scripts that only work on a single file but what I want is a script that can safely ignore LaTeX keywords and also traverse linked files...ie follow \include and \input links to produce a correct word-count for the whole document.

With vim, I currently use ggVGg CTRL+G but obviously that shows the count for the current file and does not ignore LaTeX keywords.

Does anyone know of any script (or application) that can do this job?

4
  • 4
    Trying finding a tool that counts the words in your published PDF -- most LaTeX word-counts fail on understanding what actually gets printed.
    – icio
    Commented Jun 4, 2010 at 14:25
  • 2
    @icio - Hyphenated words, math formulas, headers and footers, all make it quite difficult to count the words in a PDF.
    – Geoff
    Commented Jun 8, 2010 at 12:14
  • 1
    @Geoff - I agree, but this is a common downfall between word-counters for PDF and TeX documents so far as I am aware.
    – icio
    Commented Jun 9, 2010 at 14:21
  • 4
    Those who end up here via a search may want to look at the more recent answer on TeX.se: tex.stackexchange.com/questions/534/… Commented Jan 10, 2014 at 22:40

9 Answers 9

77

I use texcount. The webpage has a Perl script to download (and a manual).

It will include tex files that are included (\input or \include) in the document (see -inc), supports macros, and has many other nice features.

When following included files you will get detail about each separate file as well as a total. For example here is the total output for a 12 page document of mine:

TOTAL COUNT
Files: 20
Words in text: 4188
Words in headers: 26
Words in float captions: 404
Number of headers: 12
Number of floats: 7
Number of math inlines: 85
Number of math displayed: 19

If you're only interested in the total, use the -total argument.

12
  • But does it follow links to \include and \input files? Commented Jun 7, 2010 at 18:48
  • 3
    Yes, that's what the -inc parameter does (I'll edit my response).
    – Geoff
    Commented Jun 7, 2010 at 22:11
  • Brilliant. Just tested out this script and it works great! Cheers Geoff Commented Jun 8, 2010 at 16:54
  • 1
    If that's what you want, I think you can do grep bibcite paper.aux | wc, where paper.aux should be the proper aux file for your document, but you'll need to compile the document to get the aux file.
    – Geoff
    Commented Jun 24, 2013 at 12:41
  • 1
    @DmitriPisarenko - See the FAQ: app.uio.no/ifi/texcount/faq.html#languages
    – Geoff
    Commented Jul 24, 2014 at 14:04
13

I went with icio's comment and did a word-count on the pdf itself by piping the output of pdftotext to wc:

pdftotext file.pdf - | wc - w 
3
  • 3
    Be careful with this. I believe a word that is hyphenated across two lines will show up as 2 words, not one. Headers and footers will also be counted. Look at the output from pdftotext and see if it is okay for you. If you want an exact count, I would not use this solution.
    – Geoff
    Commented Jun 7, 2010 at 13:20
  • 1
    This solution is close enough if you just want to get a rough feel for how big documents are. I would agree with Geoff in that it's not suitable for holding yourself to specific publishing related word counts. Commented May 4, 2011 at 23:55
  • I like your idea because it include bibliografy items!
    – dorien
    Commented Jun 22, 2013 at 23:19
7
latex file.tex
dvips -o - file.dvi | ps2ascii | wc -w

should give you a fairly accurate word count.

2
  • If you use pdflatex, just do pdftops file.tex and then ps2ascii|wc -w I compared this count to the count in Word and of all the ones in here, it was the one with the closest number. See my comparisons in my response
    – fiacobelli
    Commented Feb 15, 2014 at 5:32
  • 1
    @fiacobelli it should be pdftops file.pdf
    – prab4th
    Commented Nov 30, 2017 at 7:53
6

To add to @aioobe,

If you use pdflatex, just do

pdftops file.pdf
ps2ascii file.ps|wc -w

I compared this count to the count in Microsoft Word in a 1599 word document (according to Word). pdftotext produced a text with 1700+ words. texcount did not include the references and produced 1088 words. ps2ascii returned 1603 words. 4 more than in Word.

I say that's a pretty good count. I am not sure where's the 4 word difference, though. :)

1
  • It's probably hyphenated words across lines or ligatures. Commented Nov 30, 2019 at 10:05
5

In Texmaker interface you can get the word count by right clicking in the PDF preview:

enter image description here

enter image description here

1
  • 2
    This gives a different number each time you run it :D Fascinating.
    – Pixel78
    Commented Jul 6, 2021 at 11:11
3

Overleaf has a word count feature:

Overleaf v2:

enter image description here

enter image description here

Overleaf v1:

enter image description here

enter image description here

1

I use the following VIM script:

function! WC()
    let filename = expand("%")
    let cmd = "detex " . filename . " | wc -w | perl -pe 'chomp; s/ +//;'"
    let result = system(cmd)
    echo result . " words"
endfunction

… but it doesn’t follow links. This would basically entail parsing the TeX file to get all linked files, wouldn’t it?

The advantage over the other answers is that it doesn’t have to produce an output file (PDF or PS) to compute the word count so it’s potentially (depending on usage) much more efficient.

Although icio’s comment is theoretically correct, I found that the above method gives quite accurate estimates for the number of words. For most texts, it’s well within the 5% margin that is used in many assignments.

1
  • Cheers for the script but following links is a must for me since my document is pretty much structured with \includes Commented Jun 4, 2010 at 14:58
1

If the use of a vim plugin suits you, the vimtex plugin has integrated the texcount tool quite nicely.

Here is an excerpt from their documentation:

:VimtexCountLetters       Shows the number of letters/characters or words in
:VimtexCountWords         the current project or in the selected region. The
                          count is created with `texcount` through a call on
                          the main project file similar to: >

                            texcount -nosub -sum [-letter] -merge -q -1 FILE
<
                          Note: Default arguments may be controlled with
                                |g:vimtex_texcount_custom_arg|.

                          Note: One may access the information through the
                                function `vimtex#misc#wordcount(opts)`, where
                                `opts` is a dictionary with the following
                                keys (defaults indicated): >

                                'range' : [1, line('$')]
                                'count_letters' : 0/1
                                'detailed' : 0
<
                                If `detailed` is 0, then it only returns the
                                total count. This makes it possible to use for
                                e.g. statusline functions. If the `opts` dict
                                is not passed, then the defaults are assumed.

                                             *VimtexCountLetters!*
                                             *VimtexCountWords!*
:VimtexCountLetters!      Similar to |VimtexCountLetters|/|VimtexCountWords|, but
:VimtexCountWords!        show separate reports for included files.  I.e.
                          presents the result of: >

                            texcount -nosub -sum [-letter] -inc FILE
<
                                             *VimtexImapsList*
                                             *<plug>(vimtex-imaps-list)*

The nice part about this is how extensible it is. On top of counting the number of words in your current file, you can make a visual selection (say two or three paragraphs) and then only apply the command to your selection.

0

For a very basic article class document I just look at the number of matches for a regex to find words. I use Sublime Text, so this method may not work for you in a different editor, but I just hit Ctrl+F (Command+F on Mac) and then, with regex enabled, search for

(^|\s+|"|((h|f|te){)|\()\w+

which should ignore text declaring a floating environment or captions on figures as well as most kinds of basic equations and \usepackage declarations, while including quotations and parentheticals. It also counts footnotes and \emphasized text and will count \hyperref links as one word. It's not perfect, but it's typically accurate to within a few dozen words or so. You could refine it to work for you, but a script is probably a better solution, since LaTeX source code isn't a regular language. Just thought I'd throw this up here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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