生成 LaTeX Wordcount 作为变量?

生成 LaTeX Wordcount 作为变量?

我正在为美国地球物理联盟撰写一篇文章,他们有一种荒谬的量化长度的方法:

对于大多数期刊,研究论文最多可包含 25 个出版单位 (PU),其中 1 个 PU 为 500 个单词或 1 个显示元素(图形或表格)。标题、作者、所属机构、要点、关键词、表格中的文本(但不包括标题)和参考文献不计入字数。较长的论文将收取超长费。Geophysical Research Letters 的研究通讯的最大长度为 12 个出版单位。较长的论文不被纳入 GRL 考虑,将被退回以缩短。对于大多数期刊,评论限制为 6 个出版单位(建议长度约为 2000 个单词和 1-2 个图形)。

我正在编写几行 LaTeX 代码来跟踪我的操作,但要将字数统计作为变量并入数学方程式有点困难。以下是我目前得到的结果:

\documentclass{article}
\usepackage[utf8]{inputenc}

% Figuring out AGU's absurd "Publication Unit" scheme:
\newcommand{\quickwordcount}[1]{%
  \immediate\write18{texcount -1 -sum -merge #1.tex> #1-words}%
  \input{#1-words}
}

\newcommand{\MyWordcount}{\quickwordcount{agujournaltemplate}}
\newcommand{\MyTestVar}{37} %for testing purposes

\usepackage{siunitx,xfp}


\usepackage{xassoccnt}
\NewTotalDocumentCounter{totalfigures}
\NewTotalDocumentCounter{totaltables}
% \NewTotalDocumentCounter{appendixchapters}
\DeclareAssociatedCounters{figure}{totalfigures}
\DeclareAssociatedCounters{table}{totaltables}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp


\title{AGU Wordcount Test}

\begin{document}

% \maketitle


There are approximately \MyWordcount words in this article. There are \TotalValue{totalfigures} figures, and \TotalValue{totaltables} tables. That means we have a total of \num[scientific-notation=false]{\fpeval{(1/500)*\MyWordcount+ \TotalValue{totalfigures} + \TotalValue{totaltables}}} publication units, out of a maximum of 25.

\end{document}

这无法编译。如果我在公式中用 \MyTestVar 替换 \MyWordcount,则编译成功。有没有什么技巧可以将 wordcount 变成变量?

答案1

您可以避免写入文件。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{siunitx,xfp}
\usepackage{catchfile}

% Figuring out AGU's absurd "Publication Unit" scheme:
\newcommand{\quickwordcount}[1]{%
  \CatchFileEdef\MyWordcount{|"texcount -1 -sum -merge #1.tex"}{\endlinechar=-1 }%
}
\quickwordcount{\jobname}

\usepackage{xassoccnt}
\NewTotalDocumentCounter{totalfigures}
\NewTotalDocumentCounter{totaltables}
% \NewTotalDocumentCounter{appendixchapters}
\DeclareAssociatedCounters{figure}{totalfigures}
\DeclareAssociatedCounters{table}{totaltables}

\title{AGU Wordcount Test}

\begin{document}

% \maketitle

\begin{figure}
\caption{a}
\end{figure}
\begin{figure}
\caption{a}
\end{figure}
\begin{figure}
\caption{a}
\end{figure}
\begin{table}
\caption{a}
\end{table}
\begin{table}
\caption{a}
\end{table}


There are approximately \MyWordcount\ words in this article. 
There are \TotalValue{totalfigures} figures, and \TotalValue{totaltables} tables.
That means we have a total of 
\num[scientific-notation=false]{%
  \fpeval{(1/500)*\MyWordcount+ \TotalValue{totalfigures} + \TotalValue{totaltables}}%
} 
publication units, out of a maximum of 25.

\end{document}

在此处输入图片描述

答案2

好吧,这还不算完整,但已经很接近了。感谢Reddit 上的 stianlybech谢谢你的帮助。此代码将字数转换为出版单位,尽管我还没有成功将它们相加(可能是因为 xassoccnt 和 pgf 不能很好地协同工作)。这对于我目前的目的来说已经足够了,但如果有人能找到一种可行的方法将计数器加到一个数字中,我将不胜感激。

\documentclass{article}
\usepackage[utf8]{inputenc}


\usepackage{xassoccnt}
\NewTotalDocumentCounter{totalfigures}
\NewTotalDocumentCounter{totaltables}
% \NewTotalDocumentCounter{appendixchapters}
\DeclareAssociatedCounters{figure}{totalfigures}
\DeclareAssociatedCounters{table}{totaltables}
% \usepackage[nomessages]{fp}% http://ctan.org/pkg/fp


% per reddit:
\def\wordcountfile#1#2{
  \immediate\write18{detex '#1.tex' |wc -w > '\jobname.wc'}

  \newread\wcinput
  \openin\wcinput=\jobname.wc

  % The group localizes the change to \endlinechar
  \begingroup
    \endlinechar=-1
    \read\wcinput to \localline

    % xdef to ensure result is visible outside the local scope
    \expandafter\xdef\csname #2\endcsname{\localline}
  \endgroup
  \closein\wcinput
}


\usepackage{pgf}

\def\calcns#1{%
\pgfkeys{/pgf/number format/precision = 1}%
\pgfmathparse{#1/500}%
\pgfmathprintnumber{\pgfmathresult}%
}

\def\PU#1#2#3{%
\pgfkeys{/pgf/number format/precision = 1}%
\pgfmathparse{#1/500+#2+#3}%
\pgfmathprintnumber{\pgfmathresult}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{AGU Wordcount Test}

\begin{document}
\wordcountfile{agujournaltemplate}{wordcount}

\maketitle





There are approximately \wordcount{} words in this article, which amounts to \calcns{\wordcount} publication units. There are \TotalValue{totalfigures} figures, and \TotalValue{totaltables} tables, which amount to one each.

% That means we have a total of \PU{\wordcount}{\TotalValue{totalfigures}}{\TotalValue{totaltables}} publication units, out of a maximum of 25.

See, that didn't work. But this does: \PU{\wordcount}{1}{0}

\begin{figure}
    \centering
    \includegraphics{}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\begin{table}[]
    \centering
    \begin{tabular}{c|c}
         &  \\
         & 
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label}
\end{table}

\end{document}

答案3

可以使用 TeXcount 模板让 TeXcount 将计数以 TeX 代码的形式写入文件。以下是将模板作为 TeXcount ( %TC:...) 指令包含在文档中的示例:

\documentclass{article}

%TC:newtemplate
%TC:template \def\CountOfAllWords{{word+hword+oword}}\n
%TC:template \def\CountOfTextWords{{word}}\n
%TC:template \def\CountOfHeaderWords{{hword}}\n

\begin{document}

\section{Word count in macro variables}

This is the text that is counted.

%TC:ignore
\section{Word count}

\immediate\write18{texcount -merge -out=\jobname.count \jobname.tex}%
\input{\jobname.count}

There are \CountOfTextWords~words in the text, and \CountOfHeaderWords~words
in headers: all words add up to \CountOfAllWords.
The `Word count' section is not included in the count.
%TC:endignore

\end{document}

现在模板将不再输出常规摘要,而是用文本和标题字数进行编写{word}{hword}替换。不同的计数器包括:word/text、headerword、otherword、header、float、inlinemath、displaymath(还有更短的别名,如 hword、oword 等)。

更多文档可在TeXcount 网站以及链接到那里的 PDF 文档,包括如何添加新的计数器。

相关内容