Command for character count including spaces (in overleaf)

Here's one idea, similar to your approach, but using egreg's answer to Read number from file :

\documentclass{article}
\newread\tmp

\newcommand{\quickcharcount}[1]{%
  \immediate\write18{texcount -1 -sum -merge -char #1.tex > #1-chars.sum}%
  \openin\tmp=#1-chars.sum%
  \read\tmp to \thechar%
  \closein\tmp%
}

\newcommand{\quickwordcount}[1]{%
  \immediate\write18{texcount -1 -sum -merge #1.tex > #1-words.sum}%
  \openin\tmp=#1-words.sum%
  \read\tmp to \theword%
  \closein\tmp%
}

\begin{document}
\quickwordcount{main}
\quickcharcount{main}

There are \thechar characters and approximately \theword spaces.
That makes approximately \the\numexpr\theword+\thechar\relax\ characters total.

\end{document}

enter image description here

The \theword and \thechar macros could be extended to include the filename argument if you need to tally up multiple files, but I've kept it fairly simple here.

Tags:

Count

Overleaf