This helpful implementation (for which we're grateful) may lack the ability to count characters or words contained within externalized content. As an example, please see this mwe.
The code is currently not compiling on my systems. I wonder if it possible there is a conflict with filecontents. Regardless, we would like to be able to count both externalized content as well as bib items which may appear in a refsection.
\documentclass{article}
\usepackage{tokcycle}[2021-03-10]
\usepackage{xcolor}
\newcounter{wordcount}
\newcounter{lettercount}
\newcounter{wordlimit}
\newif\ifinword
% USER PARAMETERS
\newif\ifrunningcount
\newif\ifsummarycount
\def\limitcolor{red}
\setcounter{wordlimit}{0}
%%
\makeatletter
% \tc@defx is like \def, but expands the replacement text once prior to assignment
\newcommand\addtomacro[2]{\tc@defx#1{#1#2}}
\newcommand\changecolor[1]{\tctestifx{.#1}{}{\addcytoks{\color{#1}{}}%
\tc@defx\currentcolor{#1}}}
\makeatother
\newcommand\dumpword{%
\addcytoks[1]{\accumword}%
\ifinword\stepcounter{wordcount}
\ifrunningcount\addcytoks[x]{$^{\thewordcount,\thelettercount}$}\fi
\ifnum\thewordcount=\value{wordlimit}\relax\changecolor{\limitcolor}\fi
\fi%
\inwordfalse
\def\accumword{}}
\newcommand\addletter[1]{%
\tctestifcatnx A#1{\stepcounter{lettercount}\inwordtrue}{\dumpword}%
\ifcountspaces\tctestifcatnx .#1{\stepcounter{lettercount}}{}\fi% NEW!!
\addtomacro\accumword{#1}}
\newif\ifcountspaces% NEW!!
\xtokcycleenvironment\countem
{\addletter{##1}}
{\dumpword\groupedcytoks{\processtoks{##1}\dumpword\expandafter}\expandafter
\changecolor\expandafter{\currentcolor}}
{\dumpword\addcytoks{##1}}
{\dumpword\ifcountspaces\stepcounter{lettercount}\fi\addcytoks{##1}}% NEW!!
{\stripgroupingtrue\def\accumword{}\def\currentcolor{.}
\setcounter{wordcount}{0}\setcounter{lettercount}{0}}
{\dumpword\retainsum\ifsummarycount\tcafterenv{%
\par(Wordcount=\thewordcount, Lettercount=\thelettercount)}\fi}
\newcommand\contentlimit[1]{\setcounter{wordlimit}{#1}}
\newcounter{globalwordcount}
\newcounter{globallettercount}
\newcommand\retainsum{%
\addtocounter{globalwordcount}{\thewordcount}%
\addtocounter{globallettercount}{\thelettercount}%
}
\newcommand\processCount{\ifnum\thegloballettercount>0%
\par Global Wordcount=\theglobalwordcount\
Global Lettercount=\thegloballettercount
\fi}
\newcommand\disablecountem{\let\countem\empty\let\endcountem\empty}
\begin{filecontents}{externalized_content.tex}
Here is some externalized content!
\end{filecontents}
\begin{document}
%\disablecountem% TO TURN OFF COUNTING, WITHOUT CHANGING DOCUMENT
\section*{Over the Limit}
\contentlimit{5}\countem
This content is over the limit.
\endcountem
\section*{Externalized content}
\contentlimit{2}\countem
\input{externalized_content.tex}%
\endcountem
\section*{Example of also counting spaces and punctuation}
\countspacestrue% NEW!!
\countem
x! , ;x\endcountem
\section*{Document analysis}
\processCount
\end{document}
Best
\countemand\endcountemhave to appear inside the external content, rather than in the main body, since the\inputis not expandable. – Steven B. Segletes Apr 26 '21 at 22:10\countemand\endcountemto the.bblfile. I am still tying to figure out how to change thethebibliographyenvironment to do this automatically for me, but have not yet met with success. – Steven B. Segletes Apr 26 '21 at 22:49