GeneralWord count display

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
MinatureCookie
Posts: 3
Joined: Sun Feb 26, 2012 3:56 pm

Word count display

Post by MinatureCookie »

Hi, sorry if this has been asked before - but everytime I try and search for information on this I get a load of information about "Count how many words in a .tex" file - whereas I want something different.

I want to, say, have something like this:

Code: Select all

\begin{wordcount}
The quick brown fox jumps over the lazy dog
\end{wordcount}
That was \wordnumber words
I've already gathered that there isn't a pre-set way to do this, so I'm happy to write my own solution... But I'm really struggling to see how. I'm worried it is just generally impossible since I'm trying something inherently imperative, and LaTeX is really lexical in nature... But there must be some way to make a package from some C++ or Perl or... Something?

Any help on this would be massively appreciated! Even if it is just a curt "No, completely impossible"

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Word count display

Post by Stefan Kottwitz »

Hi,

welcome to the board!

With XeLaTeX, you could use the xesearch package for this:

Code: Select all

\documentclass{article}
\usepackage{xesearch}
\newcounter{words}
\newenvironment{wordcount}{%
  \setcounter{words}{0}
  \SearchList!{wordcount}{\stepcounter{words}}
    {a?,b?,c?,d?,e?,f?,g?,h?,i?,j?,k?,l?,m?,
    n?,o?,p?,q?,r?,s?,t?,u?,v?,w?,x?,y?,z?}
  \UndoBoundary{'}
  \SearchOrder{p;}}{%
  \StopSearching}
\begin{document}
\begin{wordcount}
  The quick brown fox jumps over the lazy dog.
\end{wordcount}

That was \arabic{words} words.
\end{document}
wordcount.png
wordcount.png (8.5 KiB) Viewed 9679 times
Stefan
LaTeX.org admin
MinatureCookie
Posts: 3
Joined: Sun Feb 26, 2012 3:56 pm

Word count display

Post by MinatureCookie »

Thanks for the welcome :)

And ah, that's so great! Would you mind just explaining to me a few bits of the code though? I can't find much about this on Google. Namely:

Code: Select all

\SearchList!{wordcount}{\stepcounter{words}}
    {a?,b?,c?,d?,e?,f?,g?,h?,i?,j?,k?,l?,m?,
    n?,o?,p?,q?,r?,s?,t?,u?,v?,w?,x?,y?,z?}
Is this just searching for letters, and if it doesn't find one, increment 'words'? Is there any way to get it to just do this for the space character instead? Seeing as my text could include grammar/numbers.

Code: Select all

\UndoBoundary{'}
\SearchOrder{p;}
What are these two things doing?

Also - you put one too many }'s at

Code: Select all

SearchOrder{p;}}{
But thanks so much - I'm still a little new to LaTeX, I would never have found out about this in time
User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Word count display

Post by Stefan Kottwitz »

Those braces are necessary: we defined an environment, so we need arguments for begin code and end code, each in braces.

For explanation regarding xesearch and word count, have a look at the xesearch documentation, it's explained in 7.2. Word count. Click the link to it in my previous post, or type at the command prompt

Code: Select all

texdoc xesearch
to open your local documentation.

Stefan
LaTeX.org admin
MinatureCookie
Posts: 3
Joined: Sun Feb 26, 2012 3:56 pm

Re: Word count display

Post by MinatureCookie »

Ahh, I didn't see you'd linked that. And that's so great thanks, the documentation clears that all up.
Post Reply