Convert numbers in string to string
You can do it expandably, provided the string only has alphanumeric ASCII characters:
\documentclass{article}
\makeatletter
\newcommand{\convertdigits}[1]{\expandafter\convert@digits#1\convert@digits}
\def\convert@digits#1{%
\ifx#1\convert@digits
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\convert@@digits#1}%
}
\def\convert@@digits#1{%
\ifnum9=9#1%
\expandafter\@gobble
\else
\expandafter\@firstofone
\fi
{\convert@@digit#1}\convert@digits
}
\def\convert@@digit#1{%
\ifcase#1%
zero\or
one\or
two\or
three\or
four\or
five\or
six\or
seven\or
eight\or
nine\fi
}
\makeatother
\begin{document}
\def\myvar{A12Z4E}
\convertdigits{A12Z4E}
\convertdigits{\myvar}
\edef\myvartext{\convertdigits{\myvar}}
\texttt{\meaning\myvartext}
\end{document}
A different expl3
implementation: the argument to \convertdigits
is fully expanded before being examined and scanned item by item.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\convertdigits}{m}
{
\guuk_convertdigits:e { #1 }
}
\cs_new:Nn \guuk_convertdigits:n
{
\tl_map_function:nN { #1 } \guuk_digit:n
}
\cs_generate_variant:Nn \guuk_convertdigits:n { e }
\cs_new:Nn \guuk_digit:n
{
\str_case:nnF { #1 }
{
{0}{zero}
{1}{one}
{2}{two}
{3}{three}
{4}{four}
{5}{five}
{6}{six}
{7}{seven}
{8}{eight}
{9}{nine}
}
{#1}% not a digit
}
\ExplSyntaxOff
\begin{document}
\def\myvar{A12Z4E}
\convertdigits{A12Z4E}
\convertdigits{\myvar}
\edef\myvartext{\convertdigits{\myvar}}
\texttt{\meaning\myvartext}
\end{document}