0

I am currently writing a document using Overleaf, a Latex-editing service that provides an automatic word count. However, I do not know how it provides it nor how accurate it is.

I am looking for a way to include Overleaf’s total word count of my document in the output pdf automatically.

If inserting the exact number the Overleaf displays is not possible (i.e., because nobody knows how Overleaf counts words or whatever other reason), any other approximation of my document’s word count will suffice. The whole point is to avoid having to check the word count and writing it down every time I share my piece of work.

There are many questions about word count, but none that asks specifically how to include the total word count in the output pdf (and if there is one and I missed it, feel free to link me there!).

Thank you all very much for your time.

5
  • 2
    If you weren't on Overleaf, you could use the texcount command line program (see, for example, tex.stackexchange.com/q/527602/107497). I'm not sure if Overleaf would let you use the necessary \write18 and texcount. Having tagged this with overleaf, I'm pretty sure that one of their support staff will chime in at some point (possibly after the weekend).
    – Teepeemm
    Jan 22 at 22:52
  • 1
    Hello there, Tom from Overleaf Support here :) You can indeed run texcount as @Teepeemm suggests. Some details can be found in our Help at overleaf.com/learn/how-to/… and in the example at overleaf.com/latex/templates/customising-texcount/dqvrqghbtmbc -- both show how to define and use \quickwordcount{main} and \quickcharcount{main} which should do what you're looking for. Would this help?
    – yo'
    Jan 23 at 10:03
  • Thank you both for your comments. @yo': the output I obtain following your suggestion is not correct. According to Overleaf, my document has 2,548 words. However, the output of \quickwordcount{…} is 4238 (errors:105) words. My problems with \quickwordcount{…} are four: (1) it inserts a new line, which prevents me from writing “Word count: X,XXX words“ in a single line; (2) I don’t know what these 105 errors are or why they are there, (3) the count is very inaccurate, (4) there is no comma separator for the thousands. Any help with these issues?
    – Hector
    Jan 23 at 10:39
  • 1
    (2) Delete the -q from \detailtexcount and then it will show more information about those errors (I assume that will fix (3) as well). (1) If there aren't any errors, then \quickwordcount won't have the newline. (4) A comma separator will require string formatting.
    – Teepeemm
    Jan 23 at 19:35
  • Thank you for your comment. I followed your suggestions and the list of errors seems to be a sequence of !!! Invalid \verb: delimier required. !!! and !!! Encountered } without corresponding {. !!!. Moreover, at the very beginning, it says: !!! File not found or not readable: name.tex !!!. However, the document compiles just fine… Regarding (4), how do I do that “string formatting”? To be honest, I am very lost here…
    – Hector
    Jan 23 at 20:43

1 Answer 1

1

Looking through texcount's documentation, we can modify the commands from OP's link to be

\newcommand{\detailtexcount}[1]{%
  \immediate\write18{texcount -merge -sum #1.tex > #1.wcdetail }%
  \verbatiminput{#1.wcdetail}%
}

\newcommand{\quickwordcount}[1]{%
  \immediate\write18{texcount -0 -sum -merge -q #1.tex > #1-words.sum }%
  \input{#1-words.sum}% words%
}

\newcommand{\quickcharcount}[1]{%
  \immediate\write18{texcount -0 -sum -merge -char -q #1.tex > #1-chars.sum }%
  \input{#1-chars.sum}% characters (not including spaces)%
}
  • We've removed -q from \detailtexcount to show what errors are happening
  • We've changed -1 to -0 to remove a newline (and we need no errors to have no newline at all)
  • We've removed text from being output by the macro

The count can still miss generated text (for example, \lipsum paragraphs get missed entirely).

If you really need the comma, this question suggests using the packages siunitx or numprint.

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.