4

I have a wordcount macro I use to keep track of word numbers for each chapter of a book. Each chapter is written in a separate file which I input from my master file. When I work on a single chapter, I compile the text in a smaller master file of the article class, which results in a much faster compilation time. In the book, chapters are naturally defined by the \chapter{} command. In order to compile a single chapter with the article class, I map the \chapter command to \section in the preamble. Unfortunately, this breaks my wordcount macro. This has something to do with the macro looking for chapters that aren't there. The truth is I don't understand what grep and sed are doing.

Suggestions for a hack welcome. I probably need a more comprehensive way to redefine the \chapter command than the simple \let I have used. My wordcount macro for the article class (extracted from the MWE) is the following:

\newcommand\wordcount{
    \immediate\write18{texcount -sub=section \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'wordcount.log'}
\marginpar{[\input{wordcount.log}words]}}

If I manually replace each instance of \chapter by \section, the wordcount macro works. But it breaks when I redefine chapters to be sections with this shortcut:

\let\chapter\section 

MWE to be compiled with shell-escape enabled (I use XeLaTeX)

% !TeX document-id = {965bf974-53c7-4aa7-bc9b-fb76fa6b22ae}
% !TeX TXS-program:compile = txs:///xelatex/[--shell-escape]
\documentclass{article}
\let\chapter\section % article class does not understand the \chapter command
\newcommand\wordcount{
    \immediate\write18{texcount -sub=section \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'wordcount.log'}
\marginpar{[\input{wordcount.log}words]}}

\begin{document}

\chapter{Introduction}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Main Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Conclusion}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. 

\wordcount

\end{document}

enter image description here

When I manually replace \chapter by \section, it looks like this:

enter image description here

Side note: I tried to use the blindtext package to make the code of the MWE more succinct, but that does not work with the wordcount macro even with standard \section commands.

This is problably where I got my wordcount macro: Dynamically count and return number of words in a section, with other useful exchanges here: Is there any way to do a correct word count of a LaTeX document?

It wasn't too hard to adapt the wordcount macro for the book class:

\newcommand\wordcount{
    \immediate\write18{texcount -sub=section \jobname.tex  | grep "Chapter" | sed -e 's/+.*//' | sed -n \thechapter p > 'wordcount.log'}
\marginpar{[\input{wordcount.log}words]}}
3
  • 1
    \blindtext etc. can't work because the words are hidden in the macro, in my opinion
    – user31729
    Apr 22 '16 at 20:06
  • 1
    grep searches for expressions, sedis for replacing them. You should try to run your texcount first on the command line to make it work, then wrap it up in a macro. The basic error is Section -- you need Chapter, since texcount prints Chapter in the output
    – user31729
    Apr 22 '16 at 20:11
  • 1
    PatrickT -- I forgot to mention: You must replace sub=chapter, since texcount looks for this sub then. Renaming the macro does not help
    – user31729
    Apr 22 '16 at 20:24
3

See the improved version at the end of this post (2016/4/23)

The situation is a little more complicated than just renaming \chapter etc with a \let\chapter\section statement.

  • texcount looks for real sectioning units, sub=section or sub=chapter --> \chapter needs sub=chapter
  • If chapters are found, the output of `texcount is something like
File: wordcountchapters.tex
Encoding: ascii
Words in text: 409
Words in headers: 4
Words outside text (captions, etc.): 0
Number of headers: 3
Number of floats/tables/figures: 0
Number of math inlines: 0
Number of math displayed: 0
Subcounts:
  text+headers+captions (#headers/#floats/#inlines/#displayed)
  114+1+0 (1/0/0/0) Chapter: Introduction
  228+2+0 (1/0/0/0) Chapter: Main Section
  67+1+0 (1/0/0/0) Chapter: Conclusion

This means, grep must look for Chapter, not Section

→ change to "Chapter" or better grep -i "chapter" (ignoring case)

  • The next line replaces + etc. and is caught with \thesection, i.e. number of the section (or chapter). This must be replaced with some trickier statement \the\numexpr\value{chapter} (to get the correct value)

To make this work, a fake counter \let\c@chapter\c@section is introduced -- this way, the chapter counter has the same values like the section counter

I've put it all together below: the macro uses an optional argument that defaults to chapter. In other setups, use \wordcount[section].


\documentclass{article}

\let\chapter\section % article class does not understand the \chapter command
\makeatletter
\let\c@chapter\c@section % fake counter
\makeatother

\newcommand\wordcount[1][chapter]{%
  \immediate\write18{texcount -sub=#1 \jobname.tex  | grep -i "#1" | sed -e 's/+.*//' | sed -n \the\numexpr\value{#1}+1 p > 'wordcount.log'}%
  \marginpar{[\input{wordcount.log}words]}}

\begin{document}


\chapter{Introduction} 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Main Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Conclusion}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. 

\wordcount

\end{document}

enter image description here

Improved version

I've expanded the solution above to be configurable with a single command, \prepareforwordcount and \presetkeys{wc}{sub=chapter}{} (\presetkeys is from the xkeyval package)

The \prepareforwordcount sets the \chapter command etc. and \wordcount uses the preset value of the sub=... entry.

Due to a flaw of the shell - script, the \jobname mustn't contain the name of the sub unit, if chapter is requested, don't use a jobname with 'chapter' in it (texcount prints the file name and this will contain chapter, which in turn is counted by grep then).

\documentclass{article}

\usepackage{xkeyval}

\makeatletter
\define@key{wc}{sub}[section]{%
  \gdef\kvwc@sub{#1}%
}

\newcommand{\prepareforwordcount}[1][]{%
  \setkeys{wc}{#1}%
  \@ifundefined{\kvwc@sub}{%  e.g does \chapter exist?
    \expandafter\let\csname \kvwc@sub\endcsname\section%
    \expandafter\let\csname c@\kvwc@sub\endcsname\c@section% Generate a fake counter to be the same as \c@section 
  }{%
    % Nothing to be done!
  }%
}

\newcommand\wordcount[1][]{%
  \setkeys{wc}{#1}%
  \immediate\write18{texcount -sub='\kvwc@sub' \jobname.tex  | grep -i "\kvwc@sub" | sed -e 's/+.*//' | sed -n \the\numexpr\value{\kvwc@sub} p > 'wordcount.log'}%
  \marginpar{[\input{wordcount.log}words]}}

\makeatother

\presetkeys{wc}{sub=chapter}{}

\prepareforwordcount





\begin{document}

\chapter{Introduction} 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Main Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Conclusion}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. 

\wordcount

\end{document}
9
  • @PatrickT: I've updated some minutes ago, with a better version -- the proof that it works can be seen in the screenshot -- however, there's a tiny issue I've to solve...
    – user31729
    Apr 22 '16 at 20:33
  • I'll return to this later on! It's late here
    – user31729
    Apr 22 '16 at 20:38
  • Your explanations are very useful. I'm sure I can make it work on my setup sooner or later! (I have the latest texlive, updated last week)
    – PatrickT
    Apr 22 '16 at 20:40
  • 1
    @PatrickT: Perhaps you should give more details about your setup (you may write a mail (see the contact address on my profile page)
    – user31729
    Apr 23 '16 at 17:19
  • 1
    For the record, the problem was caused by the presence of invisible "invalid characters".
    – PatrickT
    Apr 27 '16 at 20:02
1

Christian Hupfer has answered my question and provided great explanations of the function of different parts of the wordcount macro I was using, and reasons why it wasn't working after I had redefined the \chapter command. Here I want to show an alternative approach I have since discovered: it relies on the package xesearch instead of the texcount program. It does not require shell-escape, but it does require XeLaTeX.

The solutions below are based on the xesearch manual (c.f. ftp://ftp.fu-berlin.de/tex/CTAN/macros/xetex/generic/xesearch/xesearch.pdf).

Example 1

The wordcount command prints the word count in the margin, on the right of where the command is placed. The word count is cumulative. In the other examples explored further down, the word counter is non-cumulative.

% !TeX document-id = {965bf974-53c7-4aa7-bc9b-fb76fa6b22ae}
% !TeX TXS-program:compile = txs:///xelatex/[--shell-escape]
\documentclass{article}
\let\chapter\section % article class does not understand the \chapter command
\usepackage{xesearch}
\newcount\wordcounter
\let\shownumber\relax
\SearchList!{wordcounter}{\global\advance\wordcounter1\shownumber{}}
{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;}
\newcommand{\wordcount}{
    \marginpar{~~[\expandafter\the\csname wordcounter\endcsname\ words]}
}

\begin{document}

\chapter{Introduction} 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Main Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Conclusion}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. 

\wordcount

\end{document}

enter image description here

The macro above gives a cumulative word count. To get a word count per section, the word counter may be reset. I have explored two approaches: provide a command to reset the counter manually and/or reset the counter automatically for each section.


Example 2

The countwords environment delimits the start and end of the word count. The donotcount{} command is used to wrap notes and other words that should not be included in the wordcount. No cumulative word count is provided.

% !TeX document-id = {965bf974-53c7-4aa7-bc9b-fb76fa6b22ae}
% !TeX TXS-program:compile = txs:///xelatex/[--shell-escape]
\documentclass{article}
\let\chapter\section % article class does not understand the \chapter command
\usepackage{xesearch}
\newcounter{wordscounter}
\newenvironment{countwords}{%
    \setcounter{wordscounter}{0}
    \SearchList!{wordcount}{\stepcounter{wordscounter}}
        {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{'’-}
    \MakeBoundary{„“‚‘–}
    \SearchOrder{p;}}{%
        \StopSearching
        \marginpar{~[\arabic{wordscounter}~words]} 
}
%%  prevent words in the argument from being included in the word count
\newcommand{\donotcount}[1]{\StopSearching #1\StartSearching}% 

\begin{document}

\chapter{Introduction} 
\begin{countwords}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.
\end{countwords}

\chapter{Main Section}
\begin{countwords}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.\footnote{\donotcount{The words of this footnote will not be included in the wordcount.}}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.
\end{countwords}

\chapter{Conclusion}
\begin{countwords}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
\end{countwords}

\end{document}

Example 3

The startwordcount command initiates the word counter. The donotcount command is used to wrap notes and other words that should not be included in the wordcount. The footnote command is redefined such that its content is not included in the word count. The wordcount commands prints the current word count in the margin. The chapter command, which is not defined in the article class, is mapped to the section command and automatically resets the wordcount counter.

% !TeX document-id = {965bf974-53c7-4aa7-bc9b-fb76fa6b22ae}
% !TeX TXS-program:compile = txs:///xelatex/[--shell-escape]
\documentclass{article}
% The article class does not understand the \chapter command
% map \chapter to \section and reset wordcounter each time \chapter is invoked
\newcommand\chapter[2][\DefaultOpt]{%
  \def\DefaultOpt{#2}%
  \section[#1]{#2}\setcounter{wordcounter}{0}%
}
\usepackage{xesearch}% must be compiled with xelatex
\newcounter{wordcounter}% define a word counter
\newcommand\startwordcount{%
    \setcounter{wordcounter}{0}
    \SearchList!{wordcount}{\stepcounter{wordcounter}}
        {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{'’-}
    \MakeBoundary{„“‚‘–}
    \SearchOrder{p;}
}
%%  print the wordcount counter in the margin
\newcommand\wordcount{%
    \marginpar{~[\arabic{wordcounter}~words]}%
}
%%  prevent words in the argument from being included in the word count
\newcommand{\donotcount}[1]{\StopSearching #1\StartSearching}% 

%%  redefine the \footnote command to take footnotes out of word count
\let\oldfootnote\footnote
\renewcommand\footnote[1]{%
\oldfootnote{\donotcount{#1}}}

\begin{document}

\startwordcount

\chapter{Introduction} 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Main Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.\footnote{\donotcount{The words of this footnote will not be included in the wordcount.}}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante. Donec ullamcorper, felis non sodales commodo, lectus velit ultrices augue, a dignissim nibh lectus placerat pede. Vivamus nunc nunc, molestie ut, ultricies vel, semper in, velit. Ut porttitor. Praesent in sapien. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis fringilla tristique neque. Sed interdum libero ut metus. Pellentesque placerat. Nam rutrum augue a leo. Morbi sed elit sit amet ante lobortis sollicitudin. Praesent blandit blandit mauris. Praesent lectus tellus, aliquet aliquam, luctus a, egestas a, turpis. Mauris lacinia lorem sit amet ipsum. Nunc quis urna dictum turpis accumsan semper.

\wordcount

\chapter{Conclusion}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 

\wordcount

\end{document}

This is a screenshot of the second and third examples:

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.