30

This closed question prompts me to propose a more general question and a solution I don't think has been demonstrated here.

When compiling with LuaLaTeX and using Unicode input, how do you set up a fallback font for specific Unicode characters that are absent from the main font?

I would welcome other approaches or critiques of the one I demonstrate.

musarithmia
  • 12,463

1 Answers1

33

We can define a command to switch to a fallback font using fontspec.

Then we can use newunicodechar to map the missing Unicode characters to a command that switched to the fallback font for those characters.

\documentclass{article}
\usepackage{fontspec, newunicodechar}
\setmainfont{PT Serif}
\newfontfamily{\fallbackfont}{Linux Libertine O}[Scale=MatchLowercase]
\DeclareTextFontCommand{\textfallback}{\fallbackfont}
\newunicodechar{ɔ}{\textfallback{ɔ}}
\newunicodechar{ϱ}{\textfallback{ϱ}}

\begin{document}
Hellɔ woϱld.
\end{document}

enter image description here

Output of pdffonts showing that both fonts are used:

name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------
NUOAQT+PTSerif-Regular               CID TrueType      yes yes yes      4  0
UAEGUX+LinLibertineO                 CID Type 0C       yes yes yes      5  0

EDIT:

We could also define a general command for setting up missing characters. It takes an optional argument which defaults to \textfallback, but a different font could be inserted in case an additional fallback font was needed.

\newcommand{\fallbackchar}[2][\textfallback]{%
    \newunicodechar{#2}{#1{#2}}%
}
\fallbackchar{ɔ}
\fallbackchar{ϱ}
musarithmia
  • 12,463
  • 5
    \DeclareTextFontCommand\textfallback{\fallbackfont} is the official way of defining \textfallback. – Manuel Jan 23 '15 at 15:08
  • @Manuel Where is this documented? I see \DeclareTextFontCommand used twice in the fontspec manual, but not explained. – musarithmia Jan 23 '15 at 15:10
  • 1
    LaTeS kernel, IIRC. – Manuel Jan 23 '15 at 15:13
  • 1
    @Manuel Thank you for improving the answer! I found the command on line 3721 of latex.ltx. I edited the answer to use this. – musarithmia Jan 23 '15 at 15:26
  • 5
    I am wondering if this could be automated somehow, so that whenever fontspec stumbles upon a character that is not in the font, it prints that character with \textfallback instead. – Gaussler Jan 23 '15 at 19:50
  • 1
    @Gaussler This might interest you: http://tex.stackexchange.com/questions/53599/standard-cjk-fonts-for-latex – musarithmia Jan 23 '15 at 20:17
  • 1
    @Gaussler And this even more: http://tex.stackexchange.com/questions/165665/detect-if-all-required-glyphs-are-available/171872#171872 – musarithmia Jan 23 '15 at 20:36