[NTG-context] Word and character count excluding TeX-directives

Rik Kabel context at rik.users.panix.com
Tue Jan 16 16:09:19 CET 2018


On 2018-01-16 09:26, Dr. Thomas Möbius wrote:
> \definestartstop
>     [abstract]
>     [style=bold,
>     after={\blank[big]}]
>
> \starttext
> \title{My title: example of a word and character count}
>
> {\strut\tfx Formal guidelines: word count of abstract: $x$, character
> count of main text: $x$, character count of figure captions: $x$.}
> \blank
>
> \startabstract
> This is the abstract. Read this and that.
> \stopabstract
>
> % start of the main text
> Some random text with formulas
>
> \startformula
> y = α + βx + ε, \quad ε \sim N(0,σ^2)
> \stopformula
>
> And there are also figures with captions.
>
> \startplacefigure[
>     location=bottom,
>     title={Residual plot with time $t$ on the x-axis and
>     residuals $e_{jt}$ on the y-axis},
>     reference={fig:subject-residual}]
> \externalfigure[residuals][height=.242\textheight]
> \stopplacefigure
>
> And some more text with $x$ and $y$ and $z$, and \placeformula
>
> \startformula \startalign
> \NC a =\NC b \NR
> \NC c =\NC d \NR
> \stopalign \stopformula
>
> And stop.
> \stoptext 

Try something based on this:

    \startluacode
         userdata = userdata or { }

         function userdata.wordcount(listname)
             filename = file.addsuffix(tex.jobname,"words")
             if lfs.isfile(filename) then
                 local w = dofile(filename)
                 if w then
                     if type(w.categories[listname]) == "table" then
                         context(w.categories[listname].total)
                     else
                         context(w.total)
                     end
                     context.par()
                 end
             end
         end
    \stopluacode
    \def\wordcount{%
         \dosingleempty\dowordcount}
    \def\dowordcount[#1]{%
         \ctxlua{userdata.wordcount("#1")}}
    \setupspellchecking[state=start,method=2]
    \ctxlua{languages.words.threshold=1}

    \definestartstop
         [abstract]
         [style=bold,
         after={\blank[big]}]

    \starttext

         \setupspellchecking[list=abstract]

         \startabstract
             This is the abstract. Read this and that.
         \stopabstract

         \setupspellchecking[list=main]

         Some random text with formulas

         \startformula
             y = α + βx + ε, \quad ε \sim N(0,σ^2)
         \stopformula

         And there are also figures with captions.

         \setupspellchecking[list=figures]

         \startplacefigure[
           location=bottom,
           title={Residual plot with time $t$ on the x-axis and
           residuals $e_{jt}$ on the y-axis},
           reference={fig:subject-residual}]
               \externalfigure[residuals][height=.242\textheight]
         \stopplacefigure

         \setupspellchecking[list=main]

         And some more text with $x$ and $y$ and $z$, and \placeformula

         \startformula \startalign
             \NC a =\NC b \NR
             \NC c =\NC d \NR
         \stopalign \stopformula

         And stop.

         \setupspellchecking[state=stop]

    \title{My title: example of a word and character count}

             Abstract: \wordcount[abstract]
             Main: \wordcount[main]
             Figures: \wordcount[figures]
             Wordcount: \wordcount

    \stoptext

-- 
Rik

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20180116/ef3cc9c3/attachment.html>


More information about the ntg-context mailing list