动态计算章节中的字数并在章节开头插入字数

动态计算章节中的字数并在章节开头插入字数

总结Einar 的第一个替代解决方案是制作一个新的输入命令,该命令自动在每个输入部分末尾生成计数,这是我用来在每章末尾生成计数的方法:

\newcommand\countinput[1]{
\input{#1}
\immediate\write18{texcount "#1.tex" -1 -sum > count.txt}
\footnote{FILE \#1 CONTAINS \input{count.txt} WORDS}
}

但是,这会创建一个新命令,意味着计算整个文档会变得更加困难,因为您使用的是“countinput”而不是“input”;但就目前而言,这对我的目的来说没问题,并且计算每个章节的问题在短期内得到了解决。

同时,不要介意需要启用 write18 ...您需要确保您已完成--shell-escape(不是--shell-escapee像 Einar 写的,我一直在尝试,urggg!)或者如果您使用 Atom,请选中以下框:

在此处输入图片描述

问题:

我正在努力适应这个建议如何动态地计算各章节中的单词数,但我想将其用于大型报告格式的文档(论文),其中显示每章中的单词数(如果可能的话,在章节的开头显示)。

他们的解决方案建议使用 texcount:

\documentclass{article}
\newcommand\wordcount{
    \immediate\write18{texcount -sub=section \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'}
(\input{count.txt}words)}

\begin{document}
\section{Introduction}
In publishing and graphic design, lorem ipsum is placeholder text (filler text) commonly used to demonstrate the graphics elements of a document or visual presentation, such as font, typography, and layout. The lorem ipsum text is typically a section of a Latin text by Cicero with words altered, added and removed that make it nonsensical in meaning and not proper Latin.

\wordcount
\section{Main Stuff}
Even though "lorem ipsum" may arouse curiosity because of its resemblance to classical Latin, it is not intended to have meaning. Where text is comprehensible in a document, people tend to focus on the textual content rather than upon overall presentation, so publishers use lorem ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text. In spite of its basis in Latin, use of lorem ipsum is often referred to as greeking, from the phrase "it's all Greek to me," which indicates that this is not meant to be readable text.

 \wordcount
\section{Conclusion}
Today's popular version of lorem ipsum was first created for Aldus Corporation's first desktop publishing program Aldus PageMaker in the mid-1980s for the Apple Macintosh. Art director Laura Perry adapted older forms of the lorem text from typography samples — it was, for example, widely used in Letraset catalogs in the 1960s and 1970s (anecdotes suggest that the original use of the "Lorem ipsum" text was by Letraset, which was used for print layouts by advertising agencies as early as the 1970s.) The text was frequently used in PageMaker templates.

\wordcount
\end{document}

但是,我的文档中有章节的主 tex 文件的工作方式如下:

\documentclass{report}
\newcommand\wordcount{
    \immediate\write18{texcount -sub=section \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'}
(\input{count.txt}words)}

\begin{document}

\chapter{Introduction}
\input{sections/introduction}
\wordcount

\chapter{Many Chapters}
\input{sections/chapters}
\wordcount

\chapter{Conclusion}
\input{sections/conclusion}
\wordcount

现在我知道 \wordcount 命令可能应该位于章节文件本身内,并且位于文件的开头(如果我需要的话),但无论哪种方式它都会失败。

并且,由于“未找到文件‘count.tex’”而失败。

如果相关的话,我会使用带有 latex 包的 atom。

有什么方法可以让我适应吗@Jake 的 解决方案适合我的用例?

编辑:

下列的艾纳解决方案,在魔术注释中使用它

--enable-write18

我现在可以让模块计算主 tex 文件中的单词数(不是很多,只是摘要),但我仍然无法让它按照原始需要用于特定章节或部分。如果我执行上述原始方法,它只会说

( words)

在输出文件中,如下所示:

像这样

编辑2:只是为了澄清,我当前的代码仍然会生成上面的空白计数输出(我在每个章节文件末尾调用 \wordcount,例如 chapter1.tex,它是通过 \input{sections/chapter1.tex} 从 main.tex 调用的),我的代码是:

在 main.tex 文件中:

% !TEX --enable-write18

\documentclass{report}

\newcommand\wordcount{
    \immediate\write18{texcount -merge -sub=section  \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'}
(\input{count.txt}words)}

\begin{document}

\chapter{Introduction}
\input{sections/introduction.tex}

\chapter{Many Chapters}
\input{sections/chapter1.tex}

\chapter{Conclusion}
\input{sections/conclusion.tex}

在每章的末尾,例如 chapter1.tex:

A little bit of intro text.

\section{Section 1}

Lorem ipsum la la la and all that.

\section{Section 2}

Lorem ipsum la la la and all that. Except this section might be longer, more lorem ipsum dolor lalala sit amet, and so on.

\wordcount

但是,这不会产生计数(如上所述再次为空白),也不适用于:

\newcommand\wordcount{
    \immediate\write18{texcount -inc -brief -sub=section  \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'}
(\input{count.txt}words)}

因此...似乎一切都与 texcount 兼容,但问题全出在我的 \newcommand 设置上......本质上,我又回到了我最初的问题——如何设置这个 \newcommand 来调用 texcount 来计算章节中的文本(例如 chapter1.tex)并将其显示在章节中?

编辑3:

一个大问题是合并命令不包括或计算 \input 章节文件的内容,只有它们的章节标题,例如 main.tex 上标准 texcount 的输出是:

File: main.tex
Encoding: ascii
Words in text: 333
Words in headers: 30
Words outside text (captions, etc.): 2
Number of headers: 12
Number of floats/tables/figures: 0
Number of math inlines: 0
Number of math displayed: 0
Subcounts:
text+headers+captions (#headers/#floats/#inlines/#displayed)
0+9+2 (1/0/0/0) _top_
333+1+0 (1/0/0/0) Chapter: Abstract
0+1+0 (1/0/0/0) Chapter: Outline
0+1+0 (1/0/0/0) Chapter: Introduction
0+2+0 (1/0/0/0) Chapter: Some Stuff
0+1+0 (1/0/0/0) Chapter: Conclusion

编辑4:

在尝试了 Einer 的替代解决方案之后,我仍然感到困惑,主要是因为 texcount 仍然没有计算来自sections目录的输入文件...例如,第一个解决方案看起来很有希望:

\newcommand\countinput[1]{
\input{#1}
\immediate\write18{texcount "#1.tex" -1 -sum > count.txt}
\footnote{FILE #1 CONTAINS \input{count.txt} WORDS}
}

(请注意,我已将 \oldinput{count.txt} 更改为 \input{count.txt},因为 \oldinput 是未定义的控制序列)

\chapter{Outline}
\countinput{sections/outline}

我仍然没有得到字数统计(并且生成了 count.txt 但是仍然是空的):

在此处输入图片描述

如果我尝试第二种替代解决方案,我会遇到更大的问题:“没有文件 b.tex”?!?!

我确信第一个替代建议会起作用,只要 texcount 能够真正计算文件中的单词数,例如“sections/file.tex”等。

编辑5:

问题似乎是双重的,Atom 不知何故无法创建和写入 count.txt 的问题也加剧了这个问题……这似乎是一个权限问题,shell-escapee 和启用 write18 都无法解决。我正在研究这个问题……但会将 Einar 的解决方案标记为合理……它基本上有效,但不适合我 :(

答案1

假设该文件count.tex未创建,则可能是中的命令\write18未运行,因此您可能需要确保您确实正在运行 TeXcount。

首先,为了\write18执行,LaTeX 必须使用命令行选项运行:--enable-write18或者--shell-escapee按照TeXcount 常见问题解答

接下来,您可以尝试不使用管道运行 TeXcount,并使用选项-out=filename将 TeXcount 的输出直接写入文件:例如,texcount -out=\jobname.out \jobname.tex给出一个最小示例。如果失败,TeXcount 可能根本就不会运行。

也许您需要提供完整路径texcount,尽管我认为这在 Linux 上不太可能成为问题。

LaTeX 日志中有任何信息吗?我认为它应该记录\write18正在运行或未运行的内容,并且如果出现严重错误,也许会提供一些错误消息。


一旦您使用 运行 TeXcount \write18,您需要使用选项运行 TeXcount,使 TeXcount 处理包含的文件,并根据您的需要生成每个文件或每个部分的统计数据。

默认情况下,TeXcount 仅解析主文件,而不解析通过\input或包含的文件\include。要让 TeXcount 处理这些文件,您需要使用以下两个选项之一:要么-inc让 TeXcount 分别解析每个文件,要么-merge让其将文件合并在一起并将其视为一个大文件进行处理。

最接近原始示例的是

texcount -merge -sub=section \jobname.tex

这将合并文件并生成每个部分的摘要计数。我认为grepsed命令可能像示例中的命令一样工作:否则,它应该经过一些调整才能工作。

我建议首先从命令行运行 TeXcount 以查看完整的输出,然后添加 greps 和 seds 以检查它们是否按预期工作。

由于您的文件位于子文件夹中,您可能需要验证\thesection每个相关点的值。

稍微不同的方法是使用 TeXcount 的每个文件统计数据,运行如下命令

texcount -inc -brief \jobname.tex

该方法应该返回每个文件一行加上总数一行。这种方法的潜在问题是您需要文件名(或路径)才能从 TeXcount 输出中提取正确的行,并且示例中的节标题将被视为主文件的一部分,而不是相应文件的一部分。


顺便提一下,还有其他方法可以提供每个部分的计数。一种方法是针对每个包含的文件运行 TeXcount,而不是针对整个文档运行,然后 grep 出相关部分。另一种方法是使用模板自定义 TeXcount 的输出,使其生成 LaTeX 代码:我会看看是否能找到或想出一个如何做到这一点的例子。

替代解决方案给出每个文件的计数

您可以定义一个替代文件输入命令来对每个文件进行计数:

\newcommand\countinput[1]{
\input{#1}
\immediate\write18{texcount "#1.tex" -1 -sum > count.txt}
\footnote{FILE #1 CONTAINS \oldinput{count.txt} WORDS}
}

您可以尝试使用 TeXcount 选项-1, -sum,-brief来找到您想要的组合。还有-template额外的自定义选项,但这可能会有点棘手。

\input您甚至可以按照以下思路重新定义现有的:

\let\oldinput=\input
\newcommand\countinput[1]{
\oldinput{#1}
\immediate\write18{texcount "#1.tex" -1 -sum > count.txt}
\footnote{FILE #1 CONTAINS \oldinput{count.txt} WORDS}
}
\let\input=\countinput

请注意,您现在必须使用\oldinput而不是\input来包含计数文件。

\verbatiminput为了进行实验,从包中使用 counts 文件可能更容易,verbatim因为 counts 文件往往包含 TeX 视为特殊字符的字符:例如“#”。这样,您可以根据需要使用 TeXcount 的完整默认输出以及每个部分的计数。

请注意,每个文件的计数不包括章节标题,因为它们是主文件的一部分,而不是包含在子文件中。

答案2

这是另一种方法。它可能不如您尝试实现的那么精确,并且需要调用环境Chapter。优点是只需要一次传递,无需写入外部文件。它使用该listofitems方法。

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern,listofitems,environ}
\makeatletter\let\gobble\@gobble\makeatother
\NewEnviron{Chapter}[1]{%
  \chapter{#1}
  \ignoreemptyitems%
  \setsepchar{ ||\par||\ }%
  \let\z\expandafter%
  \z\z\z\greadlist\z\z\z\thewordcount\z\z\z{\z\gobble\BODY}%
  \noindent Chapter word count is: \listlen\thewordcount[]%

  \z\z\z\greadlist\z\z\z\thewordcount\z\z\z{\z\gobble\BODY\ #1}%
  \noindent Chapter word count (including title) is: \listlen\thewordcount[]%
  \vspace{\baselineskip}%

  \BODY
}
\begin{document}
\begin{Chapter}{Here is a chapter}

When in the course of human events, we resort to math
\begin{equation}
 E = m c^2
\end{equation}
Note that \textit{arguments only count as a single word}!
And now we are done
\end{Chapter}

\vspace{1in}
This is how the words are digested:

\showitems*\thewordcount
\end{document}

在此处输入图片描述

相关内容