3

I'd like a warning to show up in my document as soon as it exceeds a defined length. I found how to print the the total word count of my document (assuming the main file to be main.tex):

\newcommand{\mytexcount}{%
  \immediate\write18{texcount -merge -sum -0 main.tex > main.wcdetail }%
  \verbatiminput{main.wcdetail}%
}
...
\mytexcount

further, I can print a warning dependent on a threshold condition like so:

\def\mywordthreshold{3000}

\newcommand{\countwarning}{
\ifnum [some number]>\mywordthreshold
  \emph{CAUTION: THIS TEXT SEEMS LONGER THAN \mywordthreshold WORDS}
\fi
}
...
\countwarning

However, I'm not sure, how to get the the output from \mytexcount into the condition, as it seems not to be interpreted as a number. If anyone can help with that or has an alternative suggestion, any feedback would be much appreciated!

I've created a minimal working example on Overleaf with what I've got so far. Here is its code:

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

\title{Word Count Example}

\newcommand{\mytexcount}{%
  \immediate\write18{texcount -merge -sum -0 main.tex > main.wcdetail }%
  \verbatiminput{main.wcdetail}%
}

\def\mywordthreshold{3000}

\newcommand{\countwarning}{
\ifnum 3001>\mywordthreshold
  \emph{CAUTION: THIS TEXT SEEMS LONGER THAN \mywordthreshold WORDS}
\fi
}

\begin{document}

\maketitle

\section{Ipsum}
 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\section{Word Count}
Number of words is: \mytexcount

Warning should follow this questionmark? \countwarning

\end{document}
  • I am very wary of folk who put a limit on the length of a document. Is it the number of words (but I am able to put just one word on a page)? Is it the number of words (but if a table or illustration or equation is involved are these one or many "words")? The general method employed by publishers is to count the average number of "words" on a page then multiply by the number of pages to give the overall "size" of the document. – Peter Wilson May 27 at 17:47
  • (Inspired by an answer that may not survive.) Is there a reason that you are choosing/needing to count the number of words? – Teepeemm May 27 at 18:19
  • There exist some places that require that require submissions to stay below or above a certain word count threshold. I'd like to include a flag in my standard template I use for almost everything to show up, if the criterion is not matched. In essence, I want to set a variable (the threshold) and then without having to worry about anything else have a warning pop up if the requirement is violated. – Christian Steinmeyer May 29 at 13:36
  • I tried to get \expandafter\newcommand\expandafter\doccount{\input{main.wcdetail}} but I couldn't make that (or something similar) work. You could do a simpler version of tex.stackexchange.com/a/547259/107497 if nothing else. – Teepeemm Jun 3 at 16:23
3

You can save the output of texcount in a macro using catchfile.

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

\title{Word Count Example}

\newcommand{\countwarning}[1]{%
  \CatchFileEdef\wordcount{"|texcount -merge -sum -0 \jobname.tex"}{}%
  \ifnum\wordcount>\mywordthreshold\relax
    \typeout{^^J*** WARNING ***^^J*** The file has too many words (>\mywordthreshold) ***^^J}%
  \else
    \typeout{^^J*** GOOD ***^^J*** The file is below the word limit (\mywordthreshold) ***^^J}%
  \fi
}
\newcommand\mywordthreshold{200}

\begin{document}

\maketitle

\section{Ipsum}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy 
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd 
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem 
ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod 
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd 
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem 
ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod 
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd 
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\countwarning

\end{document}

This will print on the terminal window

*** GOOD ***
*** The file is below the word limit (200) ***

If I set the threshold to 20, I get

*** WARNING ***
*** The file has too many words (>20) ***

You can easily change the definition to print something at the end of the document.

| improve this answer | |

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.