# [texhax] texhax Digest, Vol 2006, Issue 163

Reinhard Kotucha reinhard.kotucha at web.de
Tue May 16 22:50:22 CEST 2006

>>>>> "hgi" == hgi  <hgi at shaw.ca> writes:

> I have a couple of other questions - one is about variables.  How
> can I programatically determine the number of letters in a word?
> I'm hoping for something like (I'm making this up)
> \$z=LetterCount{some words in here}.  I would like to make some
> macros that could use this functionality.

\documentclass{minimal}
\newcounter{charcnt}
\makeatletter
\newcommand{\charcount}[1]{\setcounter{charcnt}{\z@}%
\makeatother

\begin{document}
\charcount{abc}\typeout{\thecharcnt}
\charcount{1234567}\typeout{\thecharcnt}
\charcount{12 34}\typeout{\thecharcnt}
\end{document}

To typeset the result, use \arabic{charcnt}.

Note that the macro counts tokens, not characters.  Since a control
sequence name is one token, \charcount{180\textdegree} will return 4.

The macro \@tforloop is defined in the LaTeX sources, file ltcntrl.dtx.

The macros \makeatletter and \makeatother temporarily allow @ in
command sequence names.  They have to be removed if you put the macro
definition into a .sty file.

\charcount doesn't work with \usepackage[utf8]{inputenc}.  It returns
the number of bytes of multibyte characters.  But if you need Unicode
you can try Omega or Aleph instead of TeX.

Regards,
Reinhard

--
----------------------------------------------------------------------------
Reinhard Kotucha			              Phone: +49-511-4592165
Marschnerstr. 25
D-30167 Hannover	                      mailto:reinhard.kotucha at web.de
----------------------------------------------------------------------------
Microsoft isn't the answer. Microsoft is the question, and the answer is NO.
----------------------------------------------------------------------------