撰写具有严格字符限制的提案。
在序言中使用以下内容
% Compile with --enable-write18 or --shell-escape options
\immediate\write18{texcount -char -inc -tex -sum <file> > <path>/count.tex}
根据需要,整个文档中应包含以下内容
%TC:ignore
%TC:endignore
以及最后的以下内容
% display information on document
\section{Document info}
\verbatiminput{<path>/count}
以下是一个 mwe:
\documentclass{article}
\usepackage{moreverb}
% Compile with --enable-write18 or --shell-escape options
\immediate\write18{texcount -char -inc -tex -sum mwe.tex > count.tex}
\begin{document}
\section*{Section A}
Content for section A
\section*{Another section with subsections}
Introduction to section
\subsection*{The first subsection}
This subsection has somewhat more voluminous content which tends to go on and on...
\subsection*{The second subsection }
Content aimed at testing whether math and symbols are counted: $\int$, $\Sigma$.
\section*{An section with externalized content}
\input{externalized.tex}
%TC:ignore
\section*{An ignored section}
\input{externalized.tex}
%TC:endignore
% display information on document
\section{Document info}
\verbatiminput{count.tex}
\end{document}
问题是,花了太多时间来解释结果。
在我看来,一个好的解决方案是这样的:
用户指定每个部分的限制/最大字符数,存储在
\def\thecharacterlimit{<limit>}
输出如下所示:
<section name>: <actual chars> / <limit no. chars>
。我认为一个常见的用例可能是用单词而不是字符来衡量等效性。表达式
<actual chars> / <limit no. chars>
根据高于或低于限制分别显示为红色或绿色。理想情况下,给定部分的结果将显示在相应部分的末尾。
可以轻松显示超出限制的增量。
话虽如此,快速修复或一些技巧将会非常有价值。
** 更新 **
我现在得到的输出是:
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (MiKTeX 20.12)
entering extended mode
(mwe.tex
LaTeX2e <2020-10-01> patch level 2
L3 programming layer <2020-12-07> xparse <2020-03-03>
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\article.cls"
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\size10.clo"))
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/filecontents\filec
ontents.sty"
Package filecontents Warning: This package is obsolete. Disabling it and
(filecontents) passing control to the filecontents environment
(filecontents) defined by the LaTeX kernel.
)'texcount' is not recognized as an internal or external command,
operable program or batch file.
LaTeX Warning: File `texcountinc.tex' already exists on the system.
Not generating it from this source.
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/xstring\xstring.st
y"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/generic/xstring\xstring.
tex"))
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\ifthen.sty")
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/xcolor\xcolor.sty"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/graphics-cfg\color
.cfg")
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/graphics-def\pdfte
x.def"))
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/hyperref\nameref.s
ty"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/refcount\refcount.
sty"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/generic/ltxcmds\ltxcmds.
sty")
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/generic/infwarerr\infwar
err.sty"))
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/generic/gettitlestring\g
ettitlestring.sty"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/kvoptions\kvoption
s.sty"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/graphics\keyval.st
y")
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/generic/kvsetkeys\kvsetk
eys.sty"))))
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/lipsum\lipsum.sty"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/l3kernel\expl3.sty
"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/l3backend\l3backen
d-pdftex.def"))
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/l3packages/xparse\
xparse.sty"
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/l3packages/xparse\
xparse-generic.tex"))
("C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\tex/latex/lipsum\lipsum.ltd.
tex")) (mwe.aux)
("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).]
) (texcountinc.tex)
\sectioncount=
有没有提示为什么这个实现不再有效? 是否有任何依赖包过期?
通过 TeXworks 和 MiKTeX,我配置排版如下
我卸载并重新安装了texcount
。我可以确认已texcount.exe
在此处安装:
C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64
安装texcount.pl
在这里:
C:\Users\Chris\AppData\Local\Programs\MiKTeX 2.9\scripts\texcount
答案1
显而易见的答案(至少对我来说)是在| scriptingprogram
之前插入>
。但是让我们假设你想要一个完全基于 TeX 的解决方案。我做了以下更改:
- 对我来说,最难的部分是
texcount
将 a#
放入其输出中。我最终决定忽略该行,只使用包含 section 的行(我也无法开始grep -v '#'
工作)。 - 似乎
-inc
没有显示输入文件的节数;但-merge
确实如此。 - 我正在使用 \jobname 来使其更具可移植性(但
\input{\jobname suffix}
会混淆 texcount)。
在每个 [sub] 节之后,您可以使用命令\withlimit{#}
为该区域设置限制。如果不这样做,您将-1
在最后得到。如果\withlimit
与节命令在同一行,texcount 会感到困惑。TeX 实际上不适用于关联数组,因此\withlimit
根据节标题定义一些命令。这意味着节标题非常脆弱。其中任何奇怪的内容(数学、其他命令等)都会弄乱一切。
我改编了逐行读取文件https://tex.stackexchange.com/a/137198/107497并使用了相当多的字符串操作。这(以及关联数组)让我想到 LaTeX3(或任何其他编程语言)方法会更简单。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{texcountinc.tex}
Input contents.
\subsection*{Input section title}
\withlimit{40}
File contents that will get input elsewhere. $a^2+b^2=c^2$.
\[\frac{\sin A}a=\frac{\sin B}b=\frac{\sin C}c\]
\end{filecontents*}
% 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*{\withlimit}[1]{%
\expandafter\newcommand\csname\@currentlabelname limit\endcsname{#1}
}
\makeatother
\usepackage{lipsum}
\begin{document}
\section*{Section A Under Limit}
\withlimit{25}
Content for section A
\section*{Another section with subsections over limit}
\withlimit{15}
Introduction to section
\subsection*{The first subsection no stated limit}
This subsection has somewhat more voluminous content which tends to go on and on...
\subsection*{Extra spaces ignored }
\withlimit{50}
Content aimed at testing whether math and symbols are counted: $\int$, $\Sigma$.
\section*{texcount doesn't understand lipsum}
\withlimit{1}
\lipsum[1]
\subsection*{Inputing Content}
\withlimit{18}
\input{texcountinc}
%TC:ignore
\section*{An ignored section}
\lipsum[2]
%TC:endignore
% display information on document
\section{Document info}
\processCount
\end{document}