1

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}
  There are \quickwordcount{main} in this article.
\end{document}
2
  • 1
    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. – imnothere Aug 31 '18 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? – crwang Aug 31 '18 at 19:07
2

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%
  \read\somefile to \@@localdummy%
  \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)
2
  • 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 '20 at 11:19
  • I have managed to get it working using the code from this Overleaf page. – avs Jul 22 '20 at 12:47

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.