[Tex/LaTex] Writing unicode characters in XeLaTeX using \char

unicodexetex

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.

Best Answer

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}

Font sample