I need to convert a letter to its index in the alphabet, and to its ASCII / Unicode index. And would like to have more than one way of achieving each of the cases (because I remember there are more than one), if possible.

First I wanted to convert a letter to its alphabet index (I remember some of the users here showed me how to do the conversion a while ago [either in the chat or in the comment section to one of the questions] but I didn't copy examples and forgot how to do it [I can't seem to find anything in the archives]), but then I decided to add ASCII- / Unicode-related index of a letter in the mix since this must be a quite similar procedure.

I recall something like '\a to reference the character a but can't seem to make it work or remember exactly what it is used for. I'll be reading manuals shortly but in the meanwhile it made sense to ask the question since it may be faster.

Thank you.

  • 1
    The syntax is <backtick><character> to get the character code of the letter. For the alphabet index you can just subtract the index of a (or A respectively).
    – siracusa
    Commented Aug 1, 2019 at 3:17

1 Answer 1


The TeXBook says:

A number in TeX's language can begin with a ', in which case it is regarded as octal, or with a ", when it is regarded as hexadecimal. Thus, \char'142 and \char"62 are equivalent to \char98.


The token `12 (left quote), when followed by any character token or by any control sequence token whose name is a single character, stands for TeX's internal code for the character in question. For example, \char`b and \char`\b are also equivalent to \char98.

And these internal codes are (from Appendix C of The TeXBook):

enter image description here

(octal numbers are represented in italics, and hexadecimal numbers in typewriter font) which is the same as the ASCII table.

So for TeX all of 98, '142, "62, and `b are valid and represent the same number.

The TeXBook also tells you what the \number primitive does:

  • \number. When TeX expands \number, it reads the number that follows (expanding tokens as it goes); the final expansion consists of the decimal representation of that number, preceded by '-' if negative.

So you can add both and have what you want! In \number`b, \number reads the number `b and expands to its decimal representation, 98, which is the ASCII code for b.

If you want the alphabetical index of such letter, you can do as siracusa suggested and subtract from the index of a (or A, if dealing with uppercase letters):

\the\numexpr`z-`a+1\relax % prints 26

(you need to add 1 because `a-`a would result in zero). Here you don't need number because \numexpr already knows that `z and `a are numbers; you just need \the to expand \numexpr.

The same goes for Unicode characters. \number`₢ (chosen at random) prints 8354, which is the decimal representation of the unicode point U+20A2. Of course you need XeTeX or LuaTeX to use these.

  • Honorable mention: \lccode and \uccode.
    – bp2017
    Commented Aug 2, 2019 at 6:54
  • 1
    @bp2017 Well, yes, those can work too. However note that you can (but shouldn't, obviously) set \lccode`b=`a, then \the\lccode`b will be 97, not 98. Also \lccode`b is (usually) equal \lccode`B, whereas \number`b and \number`B are different. Also, the \lccode of non-letter characters (\lccode`!, for example) is zero, not the ASCII index. The same goes for \uccode. Commented Aug 2, 2019 at 18:28
  • There's also \@arabic. (It can take a letter, as `CHAR, and expand to digit.)
    – bp2017
    Commented Aug 3, 2019 at 12:39
  • 1
    @bp2017 Yes because \@arabic{<stuff>} expands to \number <stuff>. And for TeX ‌`CHAR isn't a letter (although it looks like one), but a number. That's why \number (and \@arabic) works. Commented Aug 3, 2019 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .