字符/字数统计实现必须处理通过 \input{} 调用的外部化内容

字符/字数统计实现必须处理通过 \input{} 调用的外部化内容

有益的实施(对此我们深表感谢)可能缺乏计算外部化内容中所含字符或单词的能力。例如,请参阅此 mwe。

代码目前无法在我的系统上编译。我想知道是否可能与 存在冲突filecontents。无论如何,我们希望能够计算可能出现在 中的外部化内容和 bib 项目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}

最好的

相关内容