I don't know if this would help anyone, but when I was writing my thesis I wanted to do two things; (1) count the number of words for the whole thesis (instead of a single chapter), and (2) use a custom counter script. The point for the latter was that it would avoid sections such as abstracts, declarations, etc. and only select the relevant chapters.
Count words from the master file
The solution here was simple; figure out whether the file we are in is the master one, otherwise, send that to texcount
.
(defun latex-word-count-master () (interactive) (if (eq TeX-master t) (setq master (buffer-file-name)) (setq master (concat (expand-file-name TeX-master) ".tex"))) (shell-command (concat "texcount " "-dir " "-unicode " "-inc " master)))
Use a custom script
I did that by adding a custom-tex-counter
local variable to the included file pointing to the bash script that was responsible for word counting.
Declare the custom variable
(defvar custom-tex-counter nil) (make-variable-buffer-local 'custom-tex-counter) (put 'custom-tex-counter 'safe-local-variable #'stringp)
Add the path in the local variables (end of .tex
file)
%%% Local Variables: %%% mode: latex %%% TeX-master: "../thesis" %%% custom-tex-counter: "../count_words -t" %%% End:
Putting it together with the above
(defun latex-word-count-alt () (interactive) (if (eq TeX-master t) (setq master (buffer-file-name)) (setq master (concat (expand-file-name TeX-master) ".tex"))) (if (not (eq custom-tex-counter nil)) (shell-command (concat custom-tex-counter " " master)) (shell-command (concat "texcount " "-dir " "-unicode " "-inc " master))))
For reference here's what my custom script looked like (don't forget to make it executable):
#!/usr/bin/bash total='false' while getopts 't' flag; do case "$" in t) total='true' ;; ?) printf '\nUsage: %s: [-t] \n' $0; exit 2 ;; esac done shift $(($OPTIND - 1)) TOPATH=$(dirname "$") CHAPTERS=$(while read -r chapter; do printf "%s%s.tex\n" "$TOPATH" "/$chapter"; done < <(grep -Po "^[^%]\s?\\include{\K(Chapter|Appendix)[[:digit:]]+/(chapter|appendix)[[:digit:]]+" "$") \ | paste -sd' ') if [ "$total" == "false" ]; then texcount -unicode -inc $CHAPTERS else texcount -unicode -total -inc $CHAPTERS fi
Basically, the only thing this does is to grep
the non-commented chapters and appendices from the master file and count the words there.
You can change the regex for each project to match the structure you are using but, if you consistently use the same structure, you can put the bash script somewhere in your path and make it a global variable in emacs instead of a local one.