2

I'm trying to make a function that takes an argument and returns the number of words in the argument, once it is expanded.

Something along the lines of \wordcount{Some text goes here with \macros{abc} goes here.}, that expands to an arabic numeral counting the words.

It doesn't have to be too advanced, or completely perfect, I was just planning to count the number of spaces and add one. But what I'm struggling with is finding a way to fully expand that argument before counting the words. There are a couple of questions on here about fully expanding an argument, but they mostly mention \expandafter, which in my experience doesn't seem to work if there are multiple levels of macros.

What is the best way to fully expand an argument to a function, so that I can count the number of spaces in it. Or is there a better way to achieve this for this use case?

2
  • 4
    if the argument is expandable and you have a recent tex then \expandafter\wordcount\expandafter{\expanded{..........}} will fully expand the argument Commented Jul 6, 2021 at 17:19
  • 6
    \expandafter\wordcount\expanded{{..........}} works too and is a bit shorter. Commented Jul 6, 2021 at 18:01

0

You must log in to answer this question.

Browse other questions tagged .