# how to count the number of words in a sentence, the number of sentences in a paragraph, and the number of paragraph?

I am writing an article on latex.

I need to count the number of words in a sentence, the number of sentences in a paragraph, and the number of words in the paragraph?

e.g. This is the first sentence. This is the second sentence. This is the third sentence.

I would like to display this on the pdf:

(P1) This is the first sentence (1-5). This is the second sentence (2-5). This is the last sentence of the paragraph (3-8). (3-18)

Where,

P1 = paragraph #1;

(1-5) = sentence #1, words 5;

(2-5) = sentence #2, words 5;

(3-8) = sentence #3, words = 8;

and (3-12) = # sentences 3, total words = 18

@Steven or any one.

So far, we have this kind of output:

but, when you are writing an article we usually have a set of constraints in the number of words by sentence, the number of sentences by paragraph and the number of words in a paragraph.

For example, we have the rules from the book "English for writing research papers":

1) Number of words in sentence <= 35

2) Number of sentences in paragraph <= 8

3) Number of words in paragraph <= 90

So, according to these rules I would like to get red warnings in my pdf. Something like this:

That I will interpret like this:

warning red 47 : I am using more words in a sentence than those that I am allowed to.

warning red 9 : I am using more sentences in a paragraph than those I am allowed to.

warning red 96 : I am using more words in a paragraph than those than those I am allowed to.

Ideally, these values should be defined by the author. Since every author have his specific constrains.

The final color code will read:

1) blue = number of sentences

2) green = ok

3) red = warning

These type of warnings are very useful when you have a lot of pages because you can see in a glance where you probably need to cut words!!

Thank you.

• I have re-edited my answer to handle questions and exclamations appropriately. – Steven B. Segletes Jun 21 '16 at 12:56
• I have accepted your edit (adding color/superscipts). I note that you put ? and ! before the sentence ledger, but place periods . after it. Was that intentional? Also, I will re-edit to eliminate extraneous spaces there. – Steven B. Segletes Jun 21 '16 at 18:47
• Thank you for answering the question. Yes, it was intentional but it is up to you. – jorge Jun 21 '16 at 19:00
• @StevenB.Segletes. Than you so much for answering the question. I hope that you can fix the bug for: " e.g. " and 'i.e. " words. It detects them like a new sentence. Just type the sentence: I love fruits, e.g. apples and bananas. With this sentence you will see the bug. Suggestion: you can probably define a predetermined set of characters to indicate the end of a sentence, e.g. "*/." or something similar. Any was, thanks again. – jorge Jun 21 '16 at 19:01
• @StevenB.Segletes. I will edit the question to make you an important request to improve your answer. I hope you can do it because I don't know. – jorge Jun 21 '16 at 19:03

## 1 Answer

This approach uses a defined macro \startcount that ends with \stopcount to count within a multi-paragraph argument. EDITED to add \pausecount and \continuecount functionality within paragraphs, even mid-sentence. RE-EDITED to detect sentences that are questions (?) and exclamations (!).

RE-EDITED [jorge]: Superscripts are used for the parenthesis indicating the number of sentence and the number of words in the sentence, and also for the brackets indicating the number of sentences and the number of words in a paragraph.

Color code:

Blue = Numbers of sentence and number of sentences in a paragraph.

Green = Number of words in a sentence and in a paragraph.

The superscripts are required to avoid confusion with different style citing references, e.g. AMA STYLE = You et al [5] or Vancouver = You (5).

(EDITED again to replace "dangerous" non-environment \countem...\endcountem syntax with \startcount...\stopcount; also inserted \unskip\@ between end of paragraph and the paragraph ledger, so as to obtain normal spacing there)

NOTE, for aesthetics and clarity, I used brackets rather than parens to display the end-of-paragraph ledger, to differentiate it from the end-of-sentence ledger.

In general, it expects ordinary words in the argument, not macros, display math and such (though macros won't necessarily break it, if one is careful). For example, inline math can be parsed, but it will use the spaces of the input (which of course have no meaning in math mode) to count words.

Alternately, one can group the math (including display math) in braces {} and the embraced group will be counted as a single word if surrounded by spaces, or a single character, if not surrounded by spaces.

Thirdly (now shown in the MWE), within the paragraph or a sentence, one may invoke \pausecount followed by a later \continuecount to skip over counting a block of text, without disrupting the counting ledger.

I do not reset the counter myparagraphs so that if different invocations of \startcount are employed, the paragraph counter will pick up where it left off. Thus, \stopcount can be used to skip over the counting of problematic paragraphs and the counting resumed with \startcount.

\documentclass{article}
\usepackage{color}
\definecolor{Green}{rgb}{0.1,0.5,0.4}
\newcounter{totalwords}
\newcounter{mywords}
\newcounter{mysentences}
\newcounter{myparagraphs}
\setcounter{myparagraphs}{0}
\long\def\startcount#1\stopcount{%
\par\startcountpars\ignorespaces#1\par\relax}
\long\def\startcountpars#1\par#2\relax{%
\stepcounter{myparagraphs}%
\setcounter{mysentences}{0}%
\setcounter{totalwords}{0}%
(P\themyparagraphs) %
\startcountsentences#1. \relax\unskip\@ \ledger[p]{mysentences}{totalwords}\par %SuperScript + color
\ifx\relax#2\relax\else
\startcountpars#2\relax\fi
}
\def\startcountsentences#1. #2\relax{%
\hasitquestions#1? \relax% THAT ?-CHECK IS BEFORE !-CHECK HAS RAMIFICATIONS
\if T\ItHas\startcountquestions#1\relax%
\expandafter\startcountsentences\remainder. #2\relax\else
\hasitexclamations#1! \relax%
\if T\ItHas\startcountexclamations#1\relax%
\expandafter\startcountsentences\remainder. #2\relax\else
\ifx\relax#2\relax\else
\stepcounter{mysentences}%
\setcounter{mywords}{0}%
\startcountwords#1 \relax\unskip.\ledger{mysentences}{mywords}%
\startcountsentences#2\relax\fi\fi\fi
}
\def\hasitquestions#1? #2\relax{%
\ifx\relax#2\relax\def\ItHas{F}\else\def\ItHas{T}\fi
}
\def\startcountquestions#1? #2\relax{%
\hasitexclamations#1! \relax% MUST CHECK FOR ! WITHIN ?-SENTENCES
\if T\ItHas\startcountexclamations#1? #2\relax\else
\stepcounter{mysentences}%
\setcounter{mywords}{0}%
\startcountwords#1 \relax\unskip?\ledger{mysentences}{mywords}% change color
\def\remainder{#2}%
\fi
}
\def\hasitexclamations#1! #2\relax{%
\ifx\relax#2\relax\def\ItHas{F}\else\def\ItHas{T}\fi
}
\def\startcountexclamations#1! #2\relax{%
\stepcounter{mysentences}%
\setcounter{mywords}{0}%
\startcountwords#1 \relax\unskip!\ledger{mysentences}{mywords}%
\def\remainder{#2}%
}
\def\startcountwords#1 #2\relax{%
\ifx\pausecount#1{} \pausecount#2\relax\else
\stepcounter{mywords}%
\stepcounter{totalwords}%
#1 %
\ifx\relax#2\relax\else
\startcountwords#2\relax\fi
\fi
}
\def\pausecount#1\continuecount#2\relax{#1\startcountwords#2\relax}
\newcommand\ledger[3][s]{%
\def\colA{blue}\def\colB{Green}%
\if p#1\def\open{[}\def\close{]}%
\ifnum\value{#2}>8\def\colA{red}\fi\ifnum\value{#3}>90\def\colB{red}\fi\else
\def\open{(}\def\close{)}%
\ifnum\value{#2}>8\def\colA{red}\fi\ifnum\value{#3}>35\def\colB{red}\fi\fi
\allowbreak\textsuperscript{%
\open\textcolor{\colA}{\arabic{#2}}--\textcolor{\colB}{\arabic{#3}}\close} }
\def\cmd#1{\textbackslash\texttt{#1}}
\begin{document}
\def\eg{\textit{e.g.}}
\startcount
Is this the first, \eg, sentence?
This is ABSOLUTELY the second sentence!
Is this the third sentence of the paragraph?

Here we test inline math: $x^2$.
We begin a pause after this.
\pausecount
$% NOTE THAT ANY \label HERE WILL NOT BE ACCESSIBLE LATER y = mx + b$
\continuecount
Here we continue counting.
Within a paragraph, we use \cmd{pausecount}
and \cmd{continuecount} to skip over stuff.
Now here \pausecount \textit{we are pausing the count mid-sentence}
\continuecount for just a few words.
Another sentence that is only moderately long.
Another sentence that is only moderately long.
Another sentence that is very very very very very very very very very
very very very very very very very very very very very very
very very very very very very very very very very long.
Another sentence that is only moderately long.
\stopcount

To pause the count between paragraphs (as is done here), just
\cmd{stopcount} and, when ready to restart, reinvoke \cmd{startcount}.

\startcount
Here we are testing to see if we picked up where we left off in the
paragraph count.
Is it so?
Voila, it is so.
\stopcount
\end{document}


• Would you consider submitting this as an article in TUGboat? I used to write a column for it entitled "Glisterings" and if I was still doing it I would have asked for your permission to include the above. – Peter Wilson Jun 20 '16 at 18:16
• @PeterWilson I thank you for the interest. Frankly, I don't know the first thing about submitting articles to TUGboat, nor about their guidelines. But, if you think this is of general interest, perhaps you can point me in the right direction. – Steven B. Segletes Jun 20 '16 at 18:42
• @StevenB.Segletes. Than you so much for answering the question. – jorge Jun 21 '16 at 18:52
• @jorge You are welcome. This was a fun one to work on. I have cleaned up the code, to place all the superscript/color stuff into the macro \ledger. I also eliminated stray spaces, and consistently placed the punctuation before the ledger. – Steven B. Segletes Jun 21 '16 at 19:06
• @Steven B. Segletes: Thank you for considering this. It's easy to submit an article to TUGboat but details are too long for a comment here. Much information can be found at https://tug.org/TUGboat/location.html. – Peter Wilson Jun 23 '16 at 18:50