0

There is an answer explaining how to convert numbers to text here. But if I use a \ref output in the command \convertdigits, it throws the error Missing number, treated as zero. even if the ref output is a one-digit number. The code below is taken from that site.

\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}
\section{a}
\subsection{a1}\label{key}
\subsection{a2}
\section{b}
\subsection{b1}
\subsection{b2}
\subsubsection{b3}
\ref{key}

\def\myvar{2}

\convertdigits{2}

\convertdigits{\myvar}

\convertdigits{\ref{key}} % ERROR

\end{document}

My question: Can I convert the last digit of the \ref{key} to string. For example, if

the output of \ref{key} is 2.3

then

the output of \convertdigits{\ref{key}} should be three.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.