\char<number> is a TeX primitive that results in the character of ASCII code <number> with category code 12. However, it isn't expandable, unlike it's many cousins \romannumeral, \number, etc.

From my understanding, \char does not look up upper/lowercase/catcode tables, like \uppercase and \lowercase, so I'm confused as to why it unexpandable. (I'm not really sure if this is related at all, but I remember from the top of my head that it's the case for \upper/lowercase)

  • 2
    @Mico No, there are a bunch that are expandable, e.g. \expandafter. See also tex.stackexchange.com/questions/467360/… May 20, 2021 at 11:10
  • 5
    \char accesses a slot in a font and the glyphs at these slots are not necessarily representable in 8-bit encoding in a meaningful way. It's different for Unicode, where all characters are representable in UTF-8 encoding which is why \Uchar in LuaTeX is expandable. May 20, 2021 at 11:14

3 Answers 3


luatex and xetex provide \Uchar that is expandable

Note that tex has a built in mapping between external character encodings and the internal character codes, and \char access the latter so if \char expanded to a character token and files were written out and read back (for tables of contents etc) then the behaviour would be different to the current non-expandable behaviour as the characters would be re-mapped (incorrectly, most likely)

  • 3
    I would also mention \char_generate:nn of expl3 that also works with pdftex.
    – egreg
    May 20, 2021 at 12:57

Only for additional information. You can define expandable \echar{number} by this code in pdftex:

\def\hex#1{\ifcase\numexpr#1\relax 0\or 1\or 2\or 3\or 4%
   \or 5\or 6\or 7\or 8\or 9\or a\or b\or c\or d\or e\or f\fi}
\newcount\tmpa  \newcount\tmpb
\def\xstring{\expandafter\xstringA\string}  \def\xstringA#1{}
      \advance\tmpa by1\relax
      \ifnum\tmpa=16 \tmpa=0 \advance\tmpb by1 \fi
      \ifnum\tmpb<16 \repeat   
\edef\echar #1{\noexpand\ifcase\noexpand\numexpr#1\relax\echar\noexpand\fi}

This code creates \echar as macro, roughly speaking:

\def\echar#1{\ifcase#1^^00\or ^^01\or ^^02\or ... \or ^^fe\or ^^ff\fi}

where all tokens between \or have category like after \string.

  • 1
    yes, the latex format has a command pre-defined essentially like this, \char_generate:nn (the second argument specifying a catcode) May 27, 2021 at 8:55
  • 1
    It seems to be only academical discussion. Unicode engines have primitives \Uchar and \Ucharcat and there is no reason to use pdftex today. My code above shows that things are more complicated if somebody sticks on pdftex.
    – wipet
    May 27, 2021 at 12:29
  • 1
    yes char_generate:nn is \Ucharcat when it is there and a big \ifcase as you show in 8bit tex's May 27, 2021 at 12:53

David Carlisle already has explained why \char should be unexpandable.

However, there are cases in which an expandable version is handy and expl3 has the facility available. Here is a plain TeX version (needs an e-TeX engine such as pdftex, xetex or luatex).

\input expl3-generic


\cs_new:Npn \expchar #1 { \char_generate:nn { #1 } { 12 } }
\cs_new:Npn \xexpchar #1 #2 { \char_generate:nn { #1 } { #2 } }




\catcode`/=13 \def/{abc}



enter image description here

With LaTeX one has a better facility (the category code can be given as an optional argument)


  \char_generate:nn { #2 } { #1 }





\catcode`/=13 \def/{abc}




enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.