# Writing unicode characters in XeLaTeX using \char

I want to print some characters using the \char command. The Unicode is 2b45. I have already tried the code \char"2b45 but it prints some illegal character and b45. Variations with braces also don't work. Does anyone have any idea about this? I have also tried other variations, but they also print the same thing.

• You need uppercase letters here May 21, 2020 at 15:34
• A hexadecimal number consists of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. So you need to write \char"2B45  (notice the space at the end). The space is preferred: It will terminate further scanning of “future digits”. May 21, 2020 at 15:40
• Other variants are the LaTeX command \symbol{"2B45} and the escape sequence ^^^^2b45. The former will always select the character at position #x2B45 of the current font. The latter is equivalent to entering the Unicode character, and will run whatever code it triggers if it is set active. May 21, 2020 at 17:55

As Joseph Wright and others have said in the comments, you need uppercase letters with \char, so \char"2B45.

There are other options as well. The LaTeX command \symbol{"2B45} is essentially equivalent to \char, and will always display the character at slot #x2B45 of the current font. The escape sequence ^^^^2b45 (which breaks the pattern by taking lowercase letters) is equivalent to entering the Unicode character ⭅ (U+2B45). If that character is active, this form will trigger the code for it.

Here is an extremely artificial example of how you might use both, to define a symbol that generates the math symbol in math mode, with correct operator spacing, or a symbol in text mode that matches the current text formatting.

Note that the first argument to \newunicodechar should be the Unicode character itself, and it should generate a command to select the correct font and display the symbol from it.

\tracinglostchars=2 % Print a warning if a character is missing.
\documentclass{book}
\usepackage{newunicodechar}
\usepackage{unicode-math} % Or another package with \LLeftarrow, such as stix2
\usepackage[paperwidth=10cm]{geometry} % Solely to format a MWE on TeX.SX

\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{STIX Two Math}
\newfontfamily\symbolfamily{STIX Two Math}[
BoldFont = *,
BoldFeatures = {FakeBold = 1.05},
SlantedFont = *,
SlantedFeatures = {FakeSlant = 0.25},
BoldSlantedFont = *,
BoldSlantedFeatures = {FakeBold = 1.05, FakeSlant = 0.25}
]

\newunicodechar{^^^^2b45}% ⭅
{\ifmmode\LLeftarrow% In math-mode, use the math symbol
\else{\symbolfamily\symbol{"2B45}}% In text mode, change the font family
\fi}

\begin{document}
The ⭅ symbol can be used in math mode as $$a ⭅ b$$.  If we had a full font family
containing this glyph, we could write it as \textbf{bold ⭅},
\textsl{slanted ⭅}, or {\bfseries\slshape both ⭅}.
\end{document}


• I use Arial font. May 22, 2020 at 13:59
• @Someone Good for you. But seriously, you can \setmainfont to any font you want. See the fontspec manual if you need the full details. May 22, 2020 at 18:13
• I know, but either the font or compiler sees the quadruple arrow as an illegal character. May 24, 2020 at 15:07
• @Someone You need to select that one character from a font that contains it. That is, you can change the main font but keep the definition of \symbolfont the same. May 24, 2020 at 21:56

Some more words to complement Davislor's fine answer.

Using \char is not the best way to cope with the problem to begin with, because TeX will ignore a following space character. On the other hand, \symbol wouldn't.

Try \char"2B45 is nice and you'll see no space. It will be there with

\symbol{"2B45} is nice


If you want to always input the hexadecimal code and not bother with uppercase and lowercase, you can define a wrapper:

\documentclass{article}
\usepackage{fontspec}

\setmainfont{EB Garamond}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\uni}{O{12}m}
{
\char_generate:nn { \int_from_hex:n { #2 } } { #1 }
}

\ExplSyntaxOff

\begin{document}

The character \uni{2b45} is nice and so is \uni{2B46}.

\end{document}


The optional argument (default 12) can be used to generate a character with a different category code, in case of need.

• You could always just add a ~ as it is a non-breakable space. May 22, 2020 at 13:58