I am trying to implement a counter that counts the white spaces in order to give a rough estimate of the number of words contained in a paragraph.
In order to do so, I set a new counter
\newcounter{spacenumber}
and I would like for it to step forward every time a \space command is invoked.
Looking at @egreg's answer in this post: Can I redefine a command to contain itself?, I wrote
\LetLtxMacro{\oldspace}{\space}
\renewcommand{\space}{\oldspace%
\stepcounter{spacenumber}
}
but it doesn't work so far.
Here is the complete code:
\documentclass{article}% it does NOT work
\usepackage{letltxmacro}
\newcounter{spacenumber}
\LetLtxMacro{\oldspace}{\space}
\renewcommand{\space}{\oldspace%
\stepcounter{spacenumber}%
}
\begin{document}
\setcounter{spacenumber}{0}
This is an example. \thespacenumber
\end{document}
I took a look at the etoolbox package, but I still didn't find an answer because I don't know how \space is defined. In fact, I get
> \space=macro:
-> .
The question is: Am I wrong about the whole counter setup or am I wrong about how \space has to be re-defined?
\space, just don't comment the end of line after\def\space{. You are calling\spacezero times. If you want to call\spaceon each space, you need something like\obeyspaces. – Manuel Aug 15 '14 at 14:58\def\space{part... – Pier Paolo Aug 15 '14 at 15:04\space. – egreg Aug 15 '14 at 15:05