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
\begin{abstract}
Abstract text comes here...
\end{abstract}
%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:newtemplate
%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.
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:00grepandsedfrom the command? And try using-out=count.txtinstead of> count.txt? Have you made sure it actually runs texcount? There's an option that needs to be set in to allow\write18to execute externam commands (will need to look up what it was). – Einar Rødland Feb 15 '17 at 21:03\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\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-write18or--shell-escape(don't know which is the right one). – Einar Rødland Feb 15 '17 at 21:22/wordcountafter an actual section. It just doesn't work after the abstract... – Meirzev Feb 15 '17 at 21:22\newcommand\wordcount{ \immediate\write18{texcount \jobname.tex -out=count.txt} (\input{count.txt}words)}which succeeded in writing the correct word counts to count.txt, but there were errors that halted the compilation of the pdf. The errors are in relation to ./count.txt such asYou can't use macro parameter character # in horizontal modeandmissing $ inserted...I'm guessing this has something to do with the(\input{count.txt} words)bit...Thanks again for being patient! – Meirzev Feb 15 '17 at 21:40-quietmay reduce these if that's the problem. Check the output file to see how it looks. I guess the#may come from the default TeXcount output, so try using a template to customise the output. – Einar Rødland Feb 15 '17 at 21:45(File: EarlyLifeHistory.tex Encoding: ascii Words in text: 385 Words in headers: 17 Words outside text (captions, etc.): 0 Number of headers: 5 Number of floats/tables/figures: 12 Number of math inlines: 0 Number of math displayed: 0 Subcounts: text+headers+captions (headers/floats/inlines/displayed) 0+4+0 (1/0/0/0) top172+1+0(1/0/0/0)Abstract73+ 6+0(1/0/0/0)Section : Firsttoflowerinthefield51+2+0(1/6/0/0)Section...etc – Meirzev Feb 15 '17 at 21:50