[Tex/LaTex] Word count in thesis (muthesis) – how to do

packagesword count

Apologies if this is really simple but I cannot figure this out. I am using the muthesis template for my thesis. A requirement of the University is to include a word count at the end of the toc, lof and lot. Within this template there is already a line for the word count as
\def\wordcount{}

I thought this would be a case of simply using the latex word count tool online http://app.uio.no/ifi/texcount/, input each of my chapters, add the numbers up and put this into \def\wordcount{}, e.g. \def\wordcount{57103}. But when I do this, it still comes up with 'word count 999,999'.

What am I doing wrong?

Here is a MWE:

\documentclass[11pt,PhD,twoside]{muthesis}
\usepackage[T1]{fontenc}% recommended
\usepackage{textcomp}
\usepackage{todonotes}
\usepackage{amsmath}
\usepackage{amstext}

\usepackage{enumitem}
% tables
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{booktabs}
% captions and floats
\usepackage[font=footnotesize,labelfont=bf]{caption}
\usepackage[aboveskip=2pt]{subcaption} % aboveskipchanges spacing     beten     caption and figure
\captionsetup[subfigure]   {justification=justified,singlelinecheck=false}     %%% alignment of  subfigure captions

\usepackage{stackengine}
% colour, graphics, diagrams
\PassOptionsToPackage{table}{xcolor}
\usepackage{graphicx}% don't load graphics as well - this covers it all     already
\usepackage{pgfplotstable}
\pgfplotstableset{col sep=comma}
\usepackage[UKenglish]{datetime}
\usepackage{parskip}
\usepackage[none]{hyphenat}
\usepackage{lscape}

% for references

\usepackage{natbib}
%\setlength{\bibhang}{5em}
\renewcommand{\bibfont}{\normalfont\small}

\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{listings}

\usepackage[]{titlesec}
\usepackage{fancyhdr}

% to sort out issues with urls
\usepackage[hyperindex,breaklinks]{hyperref}

\titleformat{\section}
{\normalfont\fontsize{13}{15}\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}
{\normalfont\fontsize{13}{15}\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}
{\normalfont\fontsize{13}{15}\bfseries}{\thesubsubsection}{1em}{}
\titleformat{\subsubsubsection}
{\normalfont\fontsize{13}{15}\bfseries}{\thesubsubsubsection}{1em}{}

\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}

\def\afterpreface{\newpage
    \pagestyle{fancy}}
\pagestyle{fancy}
\fancyhf{}
\renewcommand*\headrulewidth{0pt}
\fancyhf[leh,roh]{\thepage}
\fancyhf[loh,reh]{\slshape\leftmark}

\begin{document}

\title{A thesis}
\author{AN Author}

\school{School of}
\faculty{Faculty}
\def\wordcount{57103} %%% Word count required at end of toc/lof and lot

\beforeabstract
\section*{Abstract}
\addcontentsline{toc}{chapter}{Abstract}

\begin{spacing}{1.0}
\fontsize{12}{12}\normalfont% faculty requirements for abstract
abstract   abstract   abstract   abstract   abstract   abstract            abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract
\end{spacing}

\afterabstract
\prefacesection{Acknowledgements}
I would like to thank......

\afterpreface

\chapter{Introduction}
\section{Background}
Background Background Background Background Background Background         Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background
\clearpage
\section{Background2}
Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background
\clearpage
\section{Background3}
Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background
\end{document}

Best Answer

So the best I can come up with is to copy the \beforeabstract macro from muthesis.cls into your preamble and adjust the word count there:

\documentclass[11pt,PhD,twoside]{muthesis}

%Your preamble goes here

\def\beforeabstract{
        \pagenumbering{arabic}
        \pagestyle{plain}
        \titlep
        \ifsigpage\signaturepage\fi
        \tableofcontents
        ~\\
        ~\\
%Here the number can be adjusted
        Word Count: 57103 
        % Download texcount.pl from  http://app.uio.no/ifi/texcount/ and
        % uncomment following lines to have word count handled automatically

        % Run texcount on report.tex and write results to word.count
        \immediate\write18{perl ./texcount.pl -1 -sum -relaxed -inc report.tex > word.count}
        % TC:ignore
        %Word Count: \input{word.count} % texcount ignore around this to stop error message (open recursion on file)
       % TC:endignore
        % note: if this causes an error (word.count missing, or a blank wordcount) you may need to add: " --enable-write18" (win) or "--shell-escape" (Linux) to arguments for LaTeX
        % see http://app.uio.no/ifi/texcount/faq.html for details of this and other possible errors
        ~\\
        \newpage
        \iftablespage
                \addvspace{10pt}
                \listoftables
                \newpage
        \fi
        \iffigurespage
                \addvspace{10pt}
                \listoffigures
                \newpage
        \fi}


\begin{document}

\title{A thesis}
\author{AN Author}

\school{School of}
\faculty{Faculty}


%You don't need that line
%\def\wordcount{57103} %%% Word count required at end of toc/lof and lot
\beforeabstract


\section*{Abstract}
\addcontentsline{toc}{chapter}{Abstract}

\begin{spacing}{1.0}
\fontsize{12}{12}\normalfont% faculty requirements for abstract
abstract   abstract   abstract   abstract   abstract   abstract            abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract   abstract
\end{spacing}

\afterabstract
\prefacesection{Acknowledgements}
I would like to thank......

\afterpreface

\chapter{Introduction}
\section{Background}
Background Background Background Background Background Background         Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background
\clearpage
\section{Background2}
Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background
\clearpage
\section{Background3}
Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background Background
\end{document}

In that way, you don't have to fiddle with the contents of muthesis, which you might want to keep as they are.

EDIT: After inspecting the commented lines in \beforeabstract I managed to do the word count automatically: For that, you need to run pdflatex --shell-escape report.tex and have the following code:

\def\beforeabstract{
        \pagenumbering{arabic}
        \pagestyle{plain}
        \titlep
        \ifsigpage\signaturepage\fi
        \tableofcontents
        ~\\
        ~\\
%        Word Count: 57103 
        % Download texcount.pl from  http://app.uio.no/ifi/texcount/ and
        % uncomment following lines to have word count handled automatically

        % Run texcount on report.tex and write results to word.count
%Insert your file here instead of "filename.tex"    
        \immediate\write18{perl ./texcount.pl -1 -sum -relaxed -inc filename.tex > word.count}
        % TC:ignore
        Word Count: \input{word.count} % texcount ignore around this to stop error message (open recursion on file)
       % TC:endignore
        % note: if this causes an error (word.count missing, or a blank wordcount) you may need to add: " --enable-write18" (win) or "--shell-escape" (Linux) to arguments for LaTeX
        % see http://app.uio.no/ifi/texcount/faq.html for details of this and other possible errors
        ~\\
        \newpage
        \iftablespage
                \addvspace{10pt}
                \listoftables
                \newpage
        \fi
        \iffigurespage
                \addvspace{10pt}
                \listoffigures
                \newpage
        \fi}

Further, you need a local copy of texcount.pl, refer to the links provided in the comments for help there.

Related Question