# Switching fonts automatically based on script, not just mixing alphabets in the same font

The following works with xelatex and produces the expected output:

\documentclass{book}
\usepackage{fontspec}

\setmainfont{DejaVu Serif}
%\setmainfont{GFS Porson}

\begin{document}

one two three

ἑν δύω τρία

\end{document}


For the Greek portions of the document, I would prefer to use the GFS Porson font. However, if I comment out the line that selects DejaVu and uncomment the following one, then it doesn't work: because Porson doesn't include the Latin alphabet, the English words just get rendered as white space on the page.

Is there some way to set this up so that I use font A for Greek and font B for English? This could be based on either the criterion of whether A is missing the relevant character, or on my saying, "Hey, xelatex, please use font A for input that's in the Latin code block of unicode, but B for input that's in the Greek code block."

Note that this differs from this previous question.

[EDIT] After some dialog and further help from Davislor, here's a non-minimal working version that I came up with:

\documentclass{article}
\tracinglostchars=2 % Warn if the current font is missing a glyph
\usepackage[paperwidth=10cm]{geometry} % Format the MWE for TeX.SX
%\usepackage[greek, english]{babel}
\usepackage{babel}
\usepackage{fontspec}
\usepackage{unicode-math}
\usepackage[GreekAndCoptic, GreekExtended]{ucharclasses}

\babelprovide[import=el-polyton]{greek}

\setDefaultTransitions{\selectlanguage{english}}{\selectlanguage{english}}
\setTransitionTo{GreekAndCoptic}{\selectlanguage{greek}}
\setTransitionTo{GreekExtended}{\selectlanguage{greek}}

\defaultfontfeatures{ Scale=MatchLowercase, Ligatures = TeX }
\defaultfontfeatures{ Scale=MatchUppercase, Ligatures=TeX }
\babelfont[greek]{rm}
{GFS Porson}
\babelfont{rm}
[Scale=1.0]{DejaVu Serif}
\babelfont{sf}
[Ligatures={Common,Discretionary}, Language=Default]{DejaVu Sans}
\babelfont{tt}
[Language=Default]{Liberation Mono}
%\setmathfont{Liberation Math}

\begin{document}
Homer’s \textit{Ὀδύσσεια} begins, “ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς
μάλα πολλὰ πλάγχθη.”
\end{document}


This produces the desired output but also outputs a bunch of warnings.

• The change you’re looking for is \babelfont[greek]{rm}{GFS Porson}. Jul 1 at 19:25
• Don't load greek as a package option if you're going to load it again with \babelprovide. In this MWE, the babelprovide command is the one you want. Jul 1 at 22:58
• Warnings about a font missing a script or language are normally harmless, but you can suppress them by, e.g., adding the options [Script=Latin,Language=Default] before {GFSPorson}. Jul 1 at 23:37
• @Davislor: I tried to do what you seemed to be describing, but it didn't work. I changed \babelfont[greek]{rm}{GFS Porson} to \babelfont[greek,Script=Latin,Language=Default]{rm}{GFS Porson}, but I still got warnings. Is that what you meant? I also get a warning saying, Font shape TU/GFSPorson(0)/m/it' undefined (Font) using TU/GFSPorson(0)/m/n'. Jul 1 at 23:47
• See the MWE in my answer. It doesn't suppress all warnings, but it suppresses that one. Or you can ignore it; it's harmless. Jul 2 at 0:16

With Babel, in LuaTeX or XeTeX, modify my answer here to override \babelfont for a specific language. Here is a MWE for LuaLaTeX:

\documentclass{article}
\tracinglostchars=2 % Warn if the current font is missing a glyph
\usepackage[paperwidth=10cm]{geometry} % Format the MWE for TeX.SX
\usepackage[english]{babel}
\usepackage{fontspec}

\babelprovide[import=el-polyton, onchar=fonts ids]{greek}

\defaultfontfeatures{ Scale=MatchUppercase, Ligatures = TeX }

\defaultfontfeatures[GFSPorson]{
ItalicFont=GFSOlga, % The GFS ships Italic faces as a separate family.
Extension={.otf} }

\babelfont{rm}
[Scale=1.0, Ligatures=Common]{DejaVu Serif}
\babelfont[greek]{rm}
[Script=Latin, Language=Default]{GFSPorson}

\begin{document}
Homer’s \textit{Ὀδύσσεια} begins, “ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς
μάλα πολλὰ πλάγχθη.”
\end{document}


Or alternatively, to make Porson the default and set \babelfont[english]{rm}. You can make the same changes as in my other answer to get this to work in XeLaTeX.

With polyglossia, set

\setmainfont{DejaVu Serif}[Scale=1.0]
\newfontfamily\greekfont{GFS Porson} % This font does not support Script=, Language= or Ligatures=.


With PDFTeX, use

\usepackage[LGR, T1]{fontenc}
\usepackage{textalpha}
\usepackage[greek,english]{babel}
\usepackage{dejavu}
\usepackage{substitutefont}

\substitutefont{LGR}{\rmdefault}{porson}


Here’s another MWE for XeLaTeX. It also suppresses some but not all warning messages, such as no sf or tt fonts being defined.

\documentclass{article}
\tracinglostchars=2 % Warn if the current font is missing a glyph
\usepackage[paperwidth=10cm]{geometry} % Format the MWE for TeX.SX
\usepackage[english]{babel}
\usepackage{fontspec}
\usepackage[GreekAndCoptic,GreekExtended]{ucharclasses}

\babelprovide[import=el-polyton]{greek}

\setDefaultTransitions{\selectlanguage{english}}{\selectlanguage{english}}
\setTransitionTo{GreekAndCoptic}{\selectlanguage{greek}}
\setTransitionTo{GreekExtended}{\selectlanguage{greek}}

\defaultfontfeatures{ Scale=MatchUppercase, Ligatures = TeX }

\defaultfontfeatures[GFSPorson]{
ItalicFont=GFSOlga, % The GFS ships Italic faces as a separate family.
Extension={.otf}  }

\babelfont{rm}
[Scale=1.0, Ligatures=Common, Language=Default]{DejaVu Serif}
\babelfont[greek]{rm}
[Language=Default]{GFSPorson}

\begin{document}
Homer’s \textit{Ὀδύσσεια} begins, “ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς
μάλα πολλὰ πλάγχθη.”
\end{document}

• Thanks for your help! I'm not able to make this work, though. Do you think you could provide a minimal working example? When I try to merge this code with the code from your previous examples, I get various errors that I don't know how to diagnose. It seems to make a difference which lines from this code sample come before or after which lines in the previous example. I also need to delete or change some lines from that example because I don't have Libertinus, but that seems to break other things. Jul 1 at 21:00
• I edited the question to supply my best attempt so far to merge the two examples as suggested, and to show the resulting errors. I've tried various things to get the errors to go away, but have had no luck. Jul 1 at 21:29
• Thanks! Actually, my question was about xelatex, not lualatex or pdftex. Your MWE doesn't work in xelatex. I was able to use the hint in your comment to get a version that works in xelatex, and I've added that to the question. It would be nice to be able to get rid of the warnings described there. Jul 1 at 23:17