# How to find the previously printed character?

Is there a way to find the previously printed character in LaTeX? E.g:

\newcommand{\th}{%
\ifthenelse%
{\equal{\previouscharacter}{1}}%
{st}%
{th}%
}

4\th and 21\th % would like to yield "4thand 21st"

-
Nice question! Nevertheless, this answer may be of interest to you. – lockstep Mar 13 '11 at 17:26
@lockstep: Actually, I just read that question and thought: wouldn't n\th look better than \nth{n}? – Tim N Mar 13 '11 at 17:30
@Tim: n\th would gobble any space that follows in the text, so ... probably not. ;-) – lockstep Mar 13 '11 at 17:33
Ah well, with some minor modifications then. – Tim N Mar 13 '11 at 17:34
I'm not sure how this would work. TeX wouldn't know that it would need to remember the character before until after it has read it... – Seamus Mar 13 '11 at 18:05

The answer, unfortunately, is no. TeX contains a few primitives that modify things before them (\over, \atop, \above and the withdelims variants), but nothing general purpose.

In addition to the various packages which implement printing ordinal numbers, I wrote a fairly straightforward macro that does this here.

-
I know nothing about \lastbox, but could it conceivably be used to do that? – Bruno Le Floch Mar 13 '11 at 23:21
@Bruno: alas, my experience as recorded in the following discussion suggests not: tex.stackexchange.com/questions/6183/…. – Ryan Reich Mar 14 '11 at 4:14

As to the specific application in question, there's the engord package. Examples:

\engordnumber{1}
\engordnumber{12}
\engordnumber{123}


return 1st, 12th, and 123rd. For the specific question, I can't answer.

-