How to expand a chardef token to a character?

This is like defining a global variable in a high level programming language and trying to use the memory register address of that variable in the same program. This will obviously not work.

Consider the simple example (in plain TeX)

%\input manmac
\newbox\foo

\foo
\bye

This will print a dotless i, because it turns out that the box register allocated is number 16 and the cmr10 font has that character in slot 16.

Now uncomment the first line and you will get a breve accent, because it turns out that the box register allocated is number 20. Why is that? Simple: manmac.tex contains four \newbox declarations.

You clearly see that you can't foresee what number the box register will correspond to, but this is of no consequence for your programs, provided you never refer to the number directly, but only with its symbolic name \foo.

The fact that \newbox\foo internally does \chardef\foo=<number> should never be exploited in programs. It's just implementation and uses the property that a \chardef token can be used in the context of a number.

Using \foo in any other context than for boxes is wrong: your output will be unpredictable, as the previous example shows.


Let's turn to a different point of view. Suppose you do

\chardef\foo=26

This is a perfectly legal instruction and plain.tex has lots of similar ones, for instance

\chardef\#=`#

(where `# refers to the ASCII code of the # character). Now \foo can be used to print the glyph occupying slot 26 in the current font. If the current font is cmr10, you get ‘æ’; if the current font is ecrm1000, you get ‘ȷ’ (dotless j).

You see that \foo does not refer to a character (considered as an abstract entity), but just to a slot: it's simply an instruction

print the glyph sitting at slot 26 in the current font

There are some conventions, of course; for instance slot 98 will contain a ‘b’ in most font; but if the current font is tcrm1000 you would get ‘ ⁀’ (a tie accent).

Even \# would have quite unpredictable results if used with some fonts: with wasy10 you get ‘○’ (a white circle).

Conclusion: comparing a \chardef token with a character makes no sense.


I'm not sure what are you exactly trying to achieve, and this seems weird, but you can test with \ifnum

\ifnum\number\foo=26 yes\else no\fi