[texhax] counting words

Oleg Katsitadze olegkat at gmail.com
Sat Sep 16 12:00:58 CEST 2006


On Fri, Sep 15, 2006 at 11:09:39PM -0400, Tom Sgouros wrote:
> Can anyone suggest a path to making a
> macro that will print the first 100 words of an argument, and not choke
> if there are only 75?

See the code below.  It's in plain TeX syntax, but I hope
you can adapt it to LaTeX.  Please note that it does not
always work correctly when there are space(s) at the
beginning / end of input.  Most importantly, if there is a
space at the beginning of the input, it'll be counted as a
word, and there may be one word too few.  It is possible to
deal with this, but of course this will make the macro more
inefficient than it already is, and I assume you generate
the input anyway, so probably it would be much more
efficient to strip leading and trailing spaces when
generating the input for TeX.

> Does this sound like the kind of thing that is
> worth attempting in a macro?

It is certainly possible, but I don't know how efficient
this would be for large inputs.

Best,
Oleg


\newcount\words
\newcount\limit

\def\answer#1{%
  \words=0
  \let\answerspace\empty
  \finanswer#1 \finish
}

\def\finanswer#1 #2\finish{%
  \advance\words by1
  \let\next\gobbleanswer
  %
  \def\temp{#2}%
  \ifx\temp\empty \else
    \ifnum\words < \limit
      \let\next\finanswer
    \fi
  \fi
  %
  \answerspace#1%
  \let\answerspace\space
  %
  \next#2\finish
}

\def\gobbleanswer#1\finish{}


\limit=20

\medskip
{\bf No spaces at the end or beginning of the input:}

$|$\answer{Now is the time for all good men to come to the aid of their
country.  And all good women, too.  And so on and so forth.}$|$

$|$\answer{Now is the time for all good men to come to the aid of their
country.  And all good women,}$|$

$|$\answer{Now is the time for all good men to come to the}$|$

$|$\answer{}$|$


\medskip
{\bf A space at the end of the input:}

$|$\answer{Now is the time for all good men to come to the aid of their
country.  And all good women, too.  And so on and so forth. }$|$

$|$\answer{Now is the time for all good men to come to the aid of their
country.  And all good women, }$|$

$|$\answer{Now is the time for all good men to come to the }$|$

$|$\answer{ }$|$


\medskip
{\bf A space at the beginning of the input:}

$|$\answer{ Now is the time for all good men to come to the aid of their
country.  And all good women, too.  And so on and so forth.}$|$

$|$\answer{ Now is the time for all good men to come to the aid of their
country.  And all good women,}$|$

$|$\answer{ Now is the time for all good men to come to the}$|$

$|$\answer{ }$|$

\bye


More information about the texhax mailing list