Word count for LaTeX within emacs

(defun latex-word-count ()
  (interactive)
  (shell-command (concat "/usr/local/bin/texcount.pl "
                         ; "uncomment then options go here "
                         (buffer-file-name))))

You may opt to put texcount.pl somewhere other than /usr/local/bin, just modify the code as appropriate if you do. This creates a new command "M-x latex-word-count", which will run texcount.pl on the current file (it will give the wrong result if you have not saved the file though). You can remove the semicolon and replace the filler text with any command line arguments you want to use, if any. You can bind this to a keyboard command with something like this in your .emacs:

(define-key latex-mode-map "\C-cw" 'latex-word-count)

The page which describes how to install texcount is here: texcount faq. Short version:

sudo cp texcount.pl /usr/local/bin/texcount.pl
or alternatively you could do as they recommend and simply name it texcount, and update the code appropriately.


Here's a slightly nicer version of the above script (handles spaces in filenames, produces one-line output, etc...) The LaTeX hooks are for AuCTeX.

(defun my-latex-setup ()
  (defun latex-word-count ()
    (interactive)
    (let* ((this-file (buffer-file-name))
           (word-count
            (with-output-to-string
              (with-current-buffer standard-output
                (call-process "texcount" nil t nil "-brief" this-file)))))
      (string-match "\n$" word-count)
      (message (replace-match "" nil nil word-count))))
    (define-key LaTeX-mode-map "\C-cw" 'latex-word-count))
(add-hook 'LaTeX-mode-hook 'my-latex-setup t)

Short version: M-! texcount <file.tex> RET

I would simply use emacs included shell-command which is

M-! <cmd> RET

together with texcount (texcount.pl) which is installed with most latex distributions. While editing your document simply hit M-! enter texcount <tex-file> and hit return.