# 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.
\engordnumber{1}