1

I am wondering if there is a way to combine latexdiff and texcount, to count the number of words that have changed between two docs?

This is not the same as the number of words before and after, but to count words changed/added/deleted.

So if a document had 100 words before, and still has 100 words, but 50 words were changed, then the output should be 50. (I cannot really think of the best way to do this. If 50 words were changed, this would show up as 50 words deleted and 50 words added. Should the count be 50? or 100?)

Since latexdiff marks up added words and deleted words, could simply count what is in this markup, perhaps.

4
  • 1
    What is the advantage of doing this in LaTeX, rather than using a separate diff utility that is well suited for such things? Sep 8 at 8:53
  • Because LaTeX will only count what are actually words, and not changes to style, etc. Plus, since I have multiple .tex files, it can do it automatically, instead of me having to copy and paste from multiple files into some other utility. And git diff will just tell me lines that have changed, not words. What other diff utility are you thinking of?
    – Paul
    Sep 8 at 9:58
  • If you use latexdiff with the --verbose option, then you get a count of added and deleted tokens (as per latexdiff notion of a token). This only gives a rough approximation, though, as words are tokens, but so is punctuation etc. Even worse, in the first pass commands are always counted as one token, even if they contain many words, and in a second pass these commands are properly expanded, but other words joined into single tokens.
    – frederik
    Sep 8 at 10:01
  • 1
    It would be relatively easy to rig up a perl script that just concatenates all the arguments of \DIFadd and \DIFdel (separately if desired) - but the result would generally not be valid latex, so I am not sure how well texcount would work on such a concatenated list.
    – frederik
    Sep 8 at 10:04
1

I'm not very familiar with latexdiff, but it seems at least some of the annotation of differences is done using \DIFadd{...} and \DIFdel{...} to indicate added and deleted text.

If that is the case, TeXcount can count these by adding macro handling rules for these two macros. One method is to include the following instructions for TeXcount in the document:

%TC:newcounter add Added
%TC:newcounter del Deleted
%TC:macro \DIFadd [add]
%TC:macro \DIFdel [del]

What these TeX comments do is provide instructions to TeXcount (%TC:...) which define two new counters, and then define macro handling rules for \DIFadd and \DIFdel which each take one argument to be counted using these new counters.

These lines need to be included in the difference file, but if you add them to the tex file they should pass through the diffing. An option is to put them in a separate file which you include, but then you need to run TeXcount with the -merge option to insert the included file.

TeXcount has an -opt option which could have been used, but unfortunately this does not work as intended: eg it does not handle the newcounter instruction.

NB: Beware that words counted as added or deleted in this manner are not included in the other word counters. It is possible to add counters together in the summary output when using templates, eg to get total number of words before and after.

3
  • This works perfectly, thanks!
    – Paul
    Sep 10 at 21:03
  • 1
    @Paul: I'm glad to hear it handled you case. Just beware that the regular word counts will no longer included words identified as added or deleted. I've added a comment to my answer to make this clear. Sep 11 at 17:03
  • Yes, I noticed this. That is not a problem for me, but thanks for updating the answer.
    – Paul
    Sep 11 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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