[texhax] Counters as ordinal numbers
Philip G. Ratcliffe
philip.ratcliffe at uninsubria.it
Mon Sep 19 11:17:52 CEST 2011
> I need to have a counter that counts like "first, second, third, fourrth,
> ...". (say up to ten thousandth). I can create a counter and put all of
> these in it just like:
>
> \def\@mycounter#1{%
> \ifcase#1\or First\or Second\or Third\or ...\else\@ctrerr\fi}
>
> \def\mycounter#1{\expandafter\@mycounter\csname c@#1\endcsname}
>
> But it will be very annoying to put each single of these in \@mycounter.
> I was wondering if you know a better solution.
The following is something I put together years ago (and it's admittedly not very kosher) for cardinals - it shouldn't be hard to modify for you needs.
It is to be used on counters just as \alph or \arabic etc. So \words{page} will give output "one" on page one.
There may well be already something more kosher on CTAN - have a look.
Cheers, Phil Ratcliffe
Put what follows in a file, say "words.sty" and then load it as a package.
\def\words#1{\@words{\@nameuse{c@#1}}}
\def\@words#1{\@tempcnta=#1\relax \@tempswafalse
\ifnum\@tempcnta=0 zero \else
\ifnum\@tempcnta<0 \multiply\@tempcnta by-1 \@tempswatrue \fi
\@tempcntb=\@tempcnta \divide\@tempcntb by1000
\ifnum\@tempcntb>19 \@ctrerr \else
\if at tempswa minus \@tempswafalse \fi
\ifnum\@tempcntb>0 \@tempswatrue \unitswords{\@tempcntb} thousand%
\multiply\@tempcntb by1000 \advance\@tempcnta by-\@tempcntb \fi
\@tempcntb=\@tempcnta \divide\@tempcntb by100
\ifnum\@tempcntb>0 \if at tempswa \space \fi
\@tempswatrue \unitswords{\@tempcntb} hundred%
\multiply\@tempcntb by100 \advance\@tempcnta by-\@tempcntb \fi
\if at tempswa \ifnum\@tempcnta>0 \space and \fi \fi
\@tempcntb=\@tempcnta \divide\@tempcntb by10
\ifnum\@tempcntb>1 \tenswords{\@tempcntb}%
\multiply\@tempcntb by10 \advance\@tempcnta by-\@tempcntb \fi
\ifnum\@tempcnta>0 \unitswords{\@tempcnta}\fi
\fi
\fi}
\def\unitswords#1{\ifcase#1\or one\or two\or three\or four\or five\or six\or
seven\or eight\or nine\or ten\or eleven\or twelve\or thirteen\or fourteen\or
fifteen\or sixteen\or seventeen\or eighteen\or nineteen\else \@ctrerr \fi}
\def\tenswords#1{\ifcase#1\or ten\or twenty\or thirty\or forty\or fifty\or
sixty\or seventy\or eighty\or ninety\else \@ctrerr \fi}
More information about the texhax
mailing list