2

I am using the following macro to count the number of words in a journal 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}

I then use:

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

to count the words between. However, I need to indicate the word count on the title page which preceeds the part of the document I want a word count for. Is there some way to insert the counter value at a point in the document that lies before the counter environment?

Thanks

Steve

3
  • Welcome to TeX - LaTeX! The usual way would be to write the value to the aux file so that it is available on the next run (just like labels…)
    – cgnieder
    Nov 19, 2015 at 14:36
  • You can use the totalcount package (part of caption) and \DeclareTotalCounter{words}. It will give you the \totalwords command. Nov 19, 2015 at 15:40
  • @clemens if either of you make your comment into an answer I can accept it. It appears I can only send this request to one poster not to both? May 4, 2016 at 11:49

1 Answer 1

2

Version 1: save the value of the counter at the end of the document to the aux file so you can read it in the next run:

% 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}

Version 2: the same but with the help of a package:

% 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}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .