Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to count how many words my LaTeX document has in it. I can do this by going to the website for the texcount package and using the web interface there. but that's not ideal.

I'd rather have some shortcut within emacs to just return number of words in a file (or ideally number of words in file and in all files called by \input or \include within the document). I have downloaded texcount script, but I don't know what to do with it. That is, I don't know where to put the .pl file, and how to call it within emacs.

That is: I want a keyboard shortcut for a shell command. And I want that shell command to run texcount on the current active buffer and return the total words in the minibuffer.

I'm using Ubuntu and emacs22, if that helps...

share|improve this question

5 Answers 5

up vote 12 down vote accepted

(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.

share|improve this answer
    
If you want to include \input and \include files in the total, add "-inc " to your options. –  Seamus Jul 2 '10 at 13:25

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)
share|improve this answer

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.

share|improve this answer

An easy combination of other solutions posted here would be:

(defun latex-word-count ()
   (interactive)
   (shell-command (concat "texcount " ; my latex installation includes texcount.pl
                       ; "uncomment then options go here, such as "
                       "-unicode "
                       "-inc "
                       (buffer-file-name))) ; better than typing path to current file
)

(define-key LaTeX-mode-map "\C-cw" 'latex-word-count)
share|improve this answer

For future reference, several of these answers would be improved by using the shell-quote-argument function to ensure that spaces and any other funny formatting in the file name would be handled correctly. For example, to improve on plgx's answer:

(defun latex-word-count ()
   (interactive)
   (shell-command (concat "texcount "
                       ; "uncomment then options go here, such as "
                       "-unicode "
                       "-inc "
                       (shell-quote-argument buffer-file-name))) 
;Now the buffer file name is sent correctly to the shell, 
;regardless of platform
)

(define-key LaTeX-mode-map "\C-cw" 'latex-word-count)
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.