I am attempting to use TeXcount to count the number of words in my abstract environment and print it out, so that when I update the abstract it prints the new word count.

I did my best to search the various forums, and I found a solution here that works for me when I'm using numbered sections:

Dynamically count and return number of words in a section

which is basically to use this macro:

    \immediate\write18{texcount -sub=section \jobname.tex  | grep "Section" |     sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'}

I attempted to change it to -sub=abstract and grep "Abstract", but the output file is empty and it prints as just ( words). The linked strategy works fine and correctly prints the number of words for my main sections, but I can't get it to work with my abstract.

I am using Knitr in Rstudio on OSX if that helps. I'm completely open to different kinds of solutions, including ones that don't involve texcount (though I'd prefer if I could do everything within my LaTeX script, similar to the above-linked solution). I am a long-time stack lurker and this is my first post, so my apologies for any newbie behavior.


Solution using original approach

Your idea of using -sub=abstract is good, but doesn't work since TeXcount doesn't actually recognise the abstract as a separate subsection. While hopefully that functionality will be added at some point, there's a quick-fix to force a new subcount of the abstract using %TC:break {name} to add breakpoints:

%TC:break Abstract
Abstract text comes here...
%TC:break main

The names Abstract and main are just arbitrary names. Now TeXcount will produce a subcount for the abstract (even without the -sub options).

It is possible to use grep, sed etc to extract and reformat the output, but it might also be helpful to give TeXcount an output template. Eg if you run TeXcount with the option -template="{sub?{title}: {word}\n?sub}" it will print only the per segment counts on the form title: words. You can use {hword}, {oword}, {sum} etc to insert word count in headers, other places, and total (as defined by the -sum option).

You can even use the template to produce TeX macros to help typeset the word count in the document. More about templates in the next solution which depends on it entirely.

Better solution!

However, there's a nicer solution which avoids having to grep out the abstract count and can allow you to shape the output in a more flexible way.

You can specify a new counter, and then a rule for the abstract environment to use this, by adding the following TeXcount instructions anywhere before the abstract, eg in the preamble:

%TC:newcounter abst Words in abstract
%TC:envir abstract [] abst

This will count words in the abstract separate from other words. I first though of using -sum=... to specify a sum count consisting only of the abstract, but that doesn't work since -sum doesn't really handle new counters very well (to be fixed I hope!).

To get the count for the abstract only, you can use an output template. This can be done in two ways. You can specify the template in the TeXcount command:

texcount -template="{abst}" file.tex

Alternatively, you can specify the template somewhere in the TeX file:

%TC:template {abst}

In either case, {abst} will be replaced by the value of the abst counter we defined.

You can even use the template to write TeX code which you can include in your document, eg using \WordsInAbstract{{abst} } as a template, but then you may need to run TeXcount with the -tex option to escape special TeX characters in the output. NB: Using {{abst}} in the template may trigger a bug where {abst} is replaced by eg 4, and then {4} gets replaced by the value in the 4th counter (number of headers), which is solved by adding an extra space.

You can also have TeXcount write the output directly to file using the -out=outfile option. Usually not a problem, but there are some cases where > outfile can't be used.

| improve this answer | |
  • Hi, thanks for taking the time to respond! Unfortunately, I wasn't able to get either of your solutions to work (I probably misunderstood how to implement). The first one (adding the breaks) works when I use the texcount command in terminal, but still returns a blank file (and prints a blank word count in my pdf) when I use my above-mentioned solution. My entire goal is to set this up so that the word counts are printed automatically within my typeset document, so I don't have to do the extra steps in the terminal. – Meirzev Feb 15 '17 at 20:58
  • The second solution also returned a blank file, and when I included the \WordsInAbstract{{abst }} in my file it failed to compile and returned a bunch of errors: overfull \hbox label `' multiply defined I'm guessing that I didn't implement this solution correctly. Is there a way I can make this work to print the number of words within the document itself? Thanks again for your help! – Meirzev Feb 15 '17 at 21:00
  • @Meirzev Did you remove the grep and sed from the command? And try using -out=count.txt instead of > count.txt? Have you made sure it actually runs texcount? There's an option that needs to be set in to allow \write18 to execute externam commands (will need to look up what it was). – Einar Rødland Feb 15 '17 at 21:03
  • I just tried it but the file isn't updating so I must have the syntax wrong. I tried this: \newcommand\wordcount{ \immediate\write18{texcount -sub=section \jobname.tex | sed -n \thesection p -out=count.txt} (\input{count.txt}words)} Also sorry I can't figure out how to format my reply properly – Meirzev Feb 15 '17 at 21:12
  • @Meirzev Just use \immediate\write18{texcount \jobname.tex -out=count.txt} without the extras. You also need to run TeX (whichever command you use to run TeX/LaTeX) with options --enable-write18 or --shell-escape (don't know which is the right one). – Einar Rødland Feb 15 '17 at 21:22

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.