# Total word count using texcount

This question is an extension of my previous question, although the answer to that question worked well in TeX-Studio, it does not work in Overleaf v2, which was primarily the Shareletax interface). It seems that \wordcount failed to return a number.

My first question is: why the original answer does not work in Overleaf?

On the other hand, I tried the minimum example below and it was able to return the correct word count in Overleaf v2. My second question is, can I somehow combine these 2 sets of codes and successfully automate total word count in Overleaf v2?

\documentclass{article}
\newcommand{\quickwordcount}[1]{%
\immediate\write18{texcount -1 -sum -merge #1.tex > #1-words}%
\input{#1-words}words%
}
\begin{document}
\end{document}

• If you click on the Overleaf menu button (or ShareLaTeX's menu button on the upper left), there is already a "Word count" option which also uses texcount. If you prefer defining \quickwordcount to set some texcount parameters: the previous solution with \jobname no longer works well, because ShareLaTeX/Overleaf v2 sets the \jobname to output. Aug 31, 2018 at 15:20
• Thanks LianTze. Is there a way I can assign \input{#1-words} as a variable in LaTeX and use it to calculate the final word count? Aug 31, 2018 at 19:07

If you click on the Overleaf menu button (or ShareLaTeX's menu button on the upper left), there is already a "Word count" option which also uses texcount. If you prefer defining \quickwordcount to set some texcount parameters: the previous solution with \jobname no longer works well, because ShareLaTeX/Overleaf v2 sets the \jobname to output.

The following works for me:

\makeatletter
\newcommand{\quickwordcount}[1]{%
\immediate\write18{texcount -1 -sum -merge #1.tex > #1-words}%
\immediate\openin\somefile=#1-words%
\immediate\closein\somefile%
\setcounter{wordcounter}{\@@localdummy}%
\@@localdummy%
}
\makeatother

\begin{document}
Total number of words: \quickwordcount{paper}
(where "paper.tex" is the name of the tex file)

• I cannot get this to work. Is this code still working with the current Overleaf? If so, can you make it into a MWE?
– avs
Jul 22, 2020 at 11:19
• I have managed to get it working using the code from this Overleaf page.
– avs
Jul 22, 2020 at 12:47