# How do I detect which font contains a character?

I use TeXWorks with XeLaTex. I want to load multiple fonts in my document. When a character doesn't exist in the main font, XeTeX should search the other fonts.

Non-working example:

\documentclass[a4paper,10pt]{article}
\usepackage{fontspec}
\setmainfont{Arial}
\newfontfamily\koreanfont{korean.ttf}
\newfontfamily\tradchinesefont{trad-chinese.ttf}
\newfontfamily\simpchinesefont{simp-chinese.ttf}
\newfontfamily\oldchinesefont{old-chinese.ttf}
\newfontfamily\greekfont{greek.ttf}
\newfontfamily\arabfont{arab.ttf}
\begin{document}
Holá hello 们 們 안녕 𦮙
\end{document}


That should produce something like this mock-up from MS Word:

Currently I get:

I can't mark up each part of the text because this needs to be dynamic. So, as in Word, I need LaTeX to recognize which font contains the character.

• Welcome to TeX.SE! – Mensch Mar 29 at 17:37
• You might look at the ucharclasses package for an alternative approach. – Davislor Mar 29 at 18:03
• The literal answer is, \iffontchar, but this would not work well if you have many different fonts to check. tex.stackexchange.com/questions/350896/… – Davislor Mar 29 at 18:03
• Another alternative: \babelfont from babel. The polyglossia package has a very similar interface to the one you want, but does not support all the languages in your example. – Davislor Mar 29 at 18:07

## 1 Answer

The literal answer to the question you asked is that \iffontchar checks whether a font contains the specified glyph, and can be used to implement a fallback. However, I think that is an XY problem.

You can almost do this with ucharclasses. For this MCVE, I used the Noto Sans font family, except for one rare ideograph that I took from Babelstone Han.

\usepackage{fontspec}
\usepackage[Latin, Arabic, CJK, Greek, Korean,
CJKUnifiedIdeographsExtensionB
]{ucharclasses}

\defaultfontfeatures{Scale = MatchLowercase, Ligatures = TeX}
\setmainfont{Noto Sans}[Scale = 1.0]
\setsansfont{Noto Sans}

\newfontfamily\koreanfont{Noto Sans CJK KR}[
Language=Korean, Script=CJK]
\newfontfamily\tradchinesefont{Noto Sans CJK TC}[
% CJKShape = Traditional,
Language=Chinese Traditional, Script = CJK]
\newfontfamily\simpchinesefont{Noto Sans CJK SC}[
% CJKShape = Simplified,
Language=Chinese Simplified, Script = CJK]
\newfontfamily\oldchinesefont{BabelStone Han}[
Script=CJK]
\newfontfamily\greekfont{Noto Sans}[
% Language = Greek,
Script = Greek]
% WARNING: RTL scripts require polyglossia or babel to work correctly!
\newfontfamily\arabfont{Noto Sans Arabic}[
Script = Arabic]

\setTransitionsForArabics{\arabfont}{}
\setTransitionsForChinese{\simpchinesefont}{}
\setTransitionsForKorean{\koreanfont}{}
\setTransitionsForGreek{\greekfont}{}
\setTransitionTo{CJKUnifiedIdeographsExtensionB}{\oldchinesefont} % For U+26B99

\begin{document}
Holá hello 们 們 안녕 𦮙
\end{document}


It doesn’t quite work out of the box for all those languages you requested. First, Simplified Chinese, Traditional Chinese and Korean share many of the same Unicode codepoints, so without further markup, you cannot tell how to render them. Second, it has some problems with right-to-left scripts such as Arabic.

If you wanted to write non-trivial amounts of all those languages in the same document, you would use a package such as Babel and write things like \foreignlanguage{korean}{韓國語} and \DeclareRobustCommand\oldchinese[1]{{\oldchinesefont #1}}.

You could declare individual glyphs with newunicodechar, as in:

\usepackage{fontspec, newunicodechar}
\newfontfamily\oldchinesefont{BabelStone Han}[
Scale = MatchLowercase,
Script=CJK]

\newunicodechar{𦮙}{{\oldchinesefont 𦮙}}


This does not play well with ucharclasses, however.