Getting word counts of specific sections

For my report, I need to add word counts to the title page for different sections, I can do a manual count but hopefully there is a better solution.

This is was my attempt but counters that update later won’t update previous calls to it. Using #total-words will give me the whole documents count but the assignment requires it for each section.

#let ReportCount = counter("ReportCount")
#let selfReviewCount = counter("selfReviewCount")

#text(size: 16pt)[
  #table(
    columns: (30%,70%),
    stroke: none,
    [Word Count:],[Work report: #context ReportCount.display() words 
(4,000 +/- 10%)\
    Self-review: #context selfReviewCount.display() words (700 minimum)\ ]
  )]

#pagebreak()

#word-count(exclude: (heading, highlight), total => [
#ReportCount.update(total.words)

Report text

])

#word-count(exclude: (heading, highlight), total => [
#selfReviewCount.update(total.words)

Self review text

])

Any help is appreciated, thanks.

Perhaps wordometer – Typst Universe can do it?

Sorry, I forgot to add the wordometer import, so it wasn’t very clear but I had been trying to use wordometer for this, but couldn’t get it to work.

Try using #context ReportCount.final() to get the value of the counter at the end of the whole document.

Also, you might want to consider using states instead of counters:

#let ReportCount = state("ReportCount")

This might be slightly easier because counters are a special kind of state that support multiple levels of counting stored in arrays, whereas state simply holds a single value.

1 Like