下列 mwe 已在这个帖子, 和这个。系统更新后,mwe 不再工作。非常感谢任何有关故障排除的指导。
W10、MikTeX 2.9、TeXWorks、安装了 Perl 5.30、安装了 grep、确认两者都已安装、通过PATH
PC 或 TeXWorks 添加了外部脚本的路径(两者似乎都做同样的事情)
我在控制台中看到的有限的调试消息是:
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/context/base/mkii\supp-p
df.mkii"
[Loading MPS to PDF converter (version 2006.09.02).]
)
\sectioncount=
以下是 mwe:
\documentclass{article}%
%\usepackage{titlesec} %**** uncomment to see error message
% Compile with --enable-write18 or --shell-escape options
\immediate\write18{texcount -char -merge -tex -sum \jobname.tex | grep -i section > \jobname Count.txt} % counts characters
%\immediate\write18{texcount -merge -tex -sum \jobname.tex | grep -i section > \jobname Count.txt} % counts words
\newcommand{\limitcount}{-1}
\usepackage{xstring}
\usepackage{ifthen}
\usepackage{xcolor}
\usepackage{nameref}
\newcommand{\processCount}{%
\newread\counts
\def\zpar{\par}
\openin\counts=\jobname Count.txt
\loop
\read\counts to \sectioncount
\ifx\sectioncount\zpar\else
\showcount{\sectioncount}\\
\fi
\ifeof\counts
\else
\repeat
}
\newcommand*{\showcount}[1]{%
% e.g. 67+18+0 (1/0/0/0) S[ubs]ection: The first subsection
\StrBehind{#1}{ection: }[\sectiontitleplusspace]
\StrGobbleRight{\sectiontitleplusspace}{1}[\sectiontitle]
\StrBefore{#1}{+}[\thiscount]
\expandafter\ifcsname\sectiontitle limit\endcsname%
\renewcommand{\limitcount}{\csname\sectiontitle limit\endcsname}%
\else%
\renewcommand{\limitcount}{-1}%
\fi%
\sectiontitle:
{%
\ifthenelse{\thiscount>\limitcount}{%
\textcolor{red}{\thiscount/\limitcount}%
\ifthenelse{\limitcount>-1}{%
\ (over by \number\numexpr\thiscount-\limitcount\relax)%
}{}%
}{%
\textcolor{green}{\thiscount/\limitcount}%
}%
}
}
\makeatletter
\newcommand*{\contentlimit}[1]{%
\expandafter\newcommand\csname\@currentlabelname limit\endcsname{#1}
}
\makeatother
\begin{document}
\section*{Over the Limit}
\contentlimit{5}
This content is over the limt.
\section*{Under the Limit}
\contentlimit{100}
More content, this time under the limit.
%TC:ignore
% display information on document
\section*{Document analysis}
\processCount
%TC:endignore
\end{document}
更新:
我验证了它texcount.pl
存在。我验证了它texcount.exe
存在。我验证了 Texworks 编译器选项包含对上述对应路径的引用。
仍然出现sectioncount=
错误。
答案1
texcount 的一个可能替代品。
根据我在这里的回答,动态计算并返回某一部分的字数,这是该问题的纯 TeX 解决方案。无需 write-18。我以多种方式调整了其他解决方案。我尝试尽可能匹配 OP 的语法。但是,仍然需要手动设置开始和停止点( )。我现在还保留了文档中出现的\countem...\endcountem
多个环境的全局制表。countem
环境countem
可以跨越段落、章节(如果需要),甚至表格、方程式等。当然,它会计算这些内容的调用语法(除非您在指定的转义分隔符之间 |转义代码|)。唯一的条件是您不能将其应用于更改其参数的 catcode 的内容(尤其是verbatim
),因为该方法涉及提前扫描所有标记……因此,在用户的宏/环境有机会更改它们之前,标记 catcode 已设置。
编辑:我已经添加了宏\disablecountem
,这样当您对计数文档的结果感到满意时,您只需在文档开头调用它即可恢复正常的非计数编译(例外:如果您使用的代码转义机制来tokcycle
暂时绕过计数,您还必须让转义令牌为\empty
。默认情况下,这是|
但可以在代码中更改为宏,使用\settcEscapechar{}
)。
应 OP 评论的要求,我进行了第二次编辑,并允许声明\countspacestrue
添加空格数和标点符号(即 cat-10 和 cat-12 标记)添加到字母计数中。查找 MWE 中标有注释的 4 行,NEW!!
以了解如何实施此更改。
甚至更新!我添加了对\obeyspaces
空格计数的支持。在 中的宏指令中\countem
,我添加了行\ifactivetok\ifnum\numberˋ##1=32\relax\stepcounter{lettercount}\fi
,因为\obeyspaces
通过激活空格标记来工作。
\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\ifactivetok\ifnum\number`##1=32\relax
\stepcounter{lettercount}\fi\fi\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{document}
%\disablecountem% TO TURN OFF COUNTING, WITHOUT CHANGING DOCUMENT
\section*{Over the Limit}
\contentlimit{5}\countem
This content is over the limit.
\endcountem
\section*{Over the Limit with running count shown}
\contentlimit{5}\runningcounttrue\countem
This content is also somewhat over the limit.
\endcountem\runningcountfalse
\section*{Under the Limit with summary count shown}
\contentlimit{100}\summarycounttrue\countem
More content, this time under the limit.
\endcountem
\section*{Example of also counting spaces and punctuation}
\countspacestrue% NEW!!
\countem
Obeyspaces is off...\endcountem
{\obeyspaces\countem
Obeyspaces is on...\endcountem}
\section*{Document analysis}
\processCount
\end{document}