回复:在乳胶文档中插入位于计数器环境之前的计数器值?

回复:在乳胶文档中插入位于计数器环境之前的计数器值?

我正在使用以下宏来计算期刊文章中的字数:

 \usepackage{xesearch}
 \newcounter{words}
 \newenvironment{wordcount}{%
 \setcounter{words}{0}
 \SearchList!{wordcount}{\stepcounter{words}}
    {a?,b?,c?,d?,e?,f?,g?,h?,i?,j?,k?,l?,m?,
    n?,o?,p?,q?,r?,s?,t?,u?,v?,w?,x?,y?,z?}
  \UndoBoundary{'}
  \SearchOrder{p;}}{%
  \StopSearching}

然后我使用:

  \begin{wordcount}
   blah blah blah  
  \end{wordcount}

来计算之间的字数。但是,我需要在文档中我想要计算字数的部分前面的标题页上指示字数。有没有办法在文档中计数器环境之前的某个位置插入计数器值?

谢谢

史蒂夫

答案1

版本 1:将文档末尾的计数器的值保存到文件中,aux以便您在下次运行中读取它:

% arara: xelatex
% arara: xelatex
\documentclass{article}

\usepackage{xesearch}
\newcounter{words}
\newenvironment{wordcount}{%
  \setcounter{words}{0}
  \SearchList!{wordcount}{\stepcounter{words}}
    {a?,b?,c?,d?,e?,f?,g?,h?,i?,j?,k?,l?,m?,
    n?,o?,p?,q?,r?,s?,t?,u?,v?,w?,x?,y?,z?}%
  \UndoBoundary{'}%
  \SearchOrder{p;}%
}{\StopSearching}

\providecommand\totalwords{}

\makeatletter
\AtEndDocument{%
  \write\@auxout{%
    \gdef\noexpand\totalwords{\arabic{words}}
  }%
}
\makeatother

\begin{document}

\totalwords

\begin{wordcount}
  blah blah blah  
\end{wordcount}

\end{document}

版本 2:相同,但借助包:

% arara: xelatex
% arara: xelatex
\documentclass{article}

\usepackage{xesearch}
\newcounter{words}
\newenvironment{wordcount}{%
  \setcounter{words}{0}
  \SearchList!{wordcount}{\stepcounter{words}}
    {a?,b?,c?,d?,e?,f?,g?,h?,i?,j?,k?,l?,m?,
    n?,o?,p?,q?,r?,s?,t?,u?,v?,w?,x?,y?,z?}%
  \UndoBoundary{'}%
  \SearchOrder{p;}%
}{\StopSearching}

\usepackage{totalcount}
\DeclareTotalCounter{words}

\begin{document}

\totalwordss % \total<counter>s

\begin{wordcount}
  blah blah blah  
\end{wordcount}

\end{document}

相关内容