Wortzählung für LaTeX innerhalb von emacs

8749
Seamus

Ich möchte zählen, wie viele Wörter mein LaTeX-Dokument enthält. Ich kann dies tun, indem ich auf die Website für das Paket texcount gehe und dort das Webinterface verwende. aber das ist nicht ideal.

Ich hätte lieber eine Verknüpfung innerhalb von emacs, um nur die Anzahl der Wörter in einer Datei (oder idealerweise die Anzahl der Wörter in der Datei und in allen Dateien, die von \inputoder \includeinnerhalb des Dokuments aufgerufen werden ) zurückzugeben. Ich habe das Skript texcount heruntergeladen, weiß aber nicht, was ich damit machen soll. Das heißt, ich weiß nicht, wo ich die .plDatei ablegen soll und wie ich sie in Emacs nennen kann.

Das heißt: Ich möchte eine Tastenkombination für einen Shell-Befehl. Und ich möchte, dass dieser Shell-Befehl texcount für den aktuell aktiven Puffer ausführt und die gesamten Wörter im Minibuffer zurückgibt.

Ich benutze Ubuntu und Emacs22, wenn das hilft ...

18

7 Antworten auf die Frage

13
Justin Smith

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

Wenn Sie \ input- und \ include-Dateien in die Gesamtsumme aufnehmen möchten, fügen Sie "-inc" zu Ihren Optionen hinzu. Seamus vor 13 Jahren 0
10
Nicholas Riley

Hier ist eine etwas schönere Version des obigen Skripts (behandelt Leerzeichen in Dateinamen, erzeugt eine Zeile usw.). Die LaTeXHooks sind für 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) 
2
plgx

Eine einfache Kombination von anderen hier veröffentlichten Lösungen wäre:

(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) 
2
Eric Greenwood

Für die zukünftige Verwendung werden einige dieser Antworten durch Verwendung der Shell-Zitat-Argument- Funktion verbessert, um sicherzustellen, dass Leerzeichen und andere witzige Formatierungen im Dateinamen korrekt verarbeitet werden. Zum Beispiel, um die Antwort von plgx zu verbessern:

(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) 
1
Flow

Kurze Version: M-! texcount <file.tex> RET

Ich würde einfach Emacs verwenden, shell-commanddas ist

M-! <cmd> RET

zusammen mit texcount(texcount.pl), das bei den meisten Latexdistributionen installiert ist. Drücken Sie während der Bearbeitung Ihres Dokuments einfach die M-!Eingabetaste texcount <tex-file>und die Eingabetaste .

0
fgregg

Sie können auch das eingebaute verwenden M-x tex-count-words. Um eine Tastenkombination zu erstellen, fügen Sie Folgendes zu hinzu.emacs

(add-hook 'latex-mode-hook (lambda () (local-set-key (kbd "C-c C-w") 'tex-count-words))) 
0
dalikan

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.