Environment that counts words inside does like the title says, but prints the words that it counts, too. Is there a way to count the words without printing them?
\countCommand{Four and twenty blackbirds} % should print: 4
Environment that counts words inside does like the title says, but prints the words that it counts, too. Is there a way to count the words without printing them?
\countCommand{Four and twenty blackbirds} % should print: 4
Here's a simple solution using xstring
% arara: pdflatex
\documentclass{article}
\usepackage{xstring}
\newcommand{\wordcount}[1]{\StrCount{#1}{\space}[\tmp]%
\number\numexpr\tmp+1\relax}
\begin{document}
\wordcount{Four and twenty blackbirds}
\end{document}
It works by counting the spaces, and then adding 1; it works fine even when there are multiple spaces between words, so you can use
\wordcount{Four and twenty blackbirds}
and still get 4. However if you try anything like
\wordcount{ Four and twenty blackbirds}
\wordcount{Four and twenty blackbirds }
\wordcount{ Four and twenty blackbirds }
then you won't get the expected result. You can fix this using, for example, something like the following:
\newcount\cmh
\newcommand{\wordcount}[1]{%
\StrCount{#1}{\space}[\tmp]%
\cmh=\tmp%
\IfBeginWith{#1}{\space}{\advance\cmh by -1\relax}{}%
\IfEndWith{#1}{\space}{\advance\cmh by -1\relax}{}%
\number\numexpr\cmh+1\relax
}
If you mean counting the spaces in the argument, at all brace levels, with l3regex you can do it:
\documentclass{article}
\usepackage{xparse,l3regex}
\ExplSyntaxOn
\NewDocumentCommand{\countwords}{+m} % allow \par (or blank lines in the argument)
{
\regex_count:nnN { \s } { #1 } \l_corneli_words_int
\int_to_arabic:n { \l_corneli_words_int + 1 }
}
\int_new:N \l_corneli_words_int
\ExplSyntaxOff
\begin{document}
\countwords{Four and twenty blackbirds}
\countwords{Four \emph{and twenty} blackbirds}
\end{document}
This prints
4
4
\par, just type {+m} instead of {m} in the definition of \countwords.
– egreg
Mar 03 '14 at 13:18
\documentclass{article}
\usepackage{readarray}
\begin{document}
\getargsC{Four and twenty blackbirds}
Words = \narg\par
The words are \argi, \argii, \argiii, and \argiv.
\end{document}
Thus, based on this MWE, \def\wordcount#1{\getargsC{#1}\narg} would suffice to answer the OP's question. Note that \getargsC is functionally equivalent but far superior in speed to the \getargs macro of the stringstrings package.
\emph{...} is treated as a single word, precisely because that is the way LaTeX treats groups (think about how we define arguments); however, \itshape four and twenty blackbirds would pickup the proper number of words.
– Steven B. Segletes
Mar 03 '14 at 04:25