# What does ucharclasses do here?

I'm trying to understand how to use the ucharclasses package. See this example:

\documentclass{article}
\usepackage{fontspec}
\usepackage{ucharclasses}
\newfontfamily\latinfont{Arial}
\newfontfamily\unicodefont{Times New Roman}

\setTransitionsForLatin{\latinfont}{\unicodefont}

\begin{document}
Doppelgänger
\end{document}


Output:

(I chose two fonts that are easily distinguishable for the example.)

Why is "Doppelgä" (including the "ä") rendered in \latinfont and "nger" is rendered in \unicodefont?

• ä is in the extended latin block and n is in the basic latin block so there is a transition there, but you would have hoped \setTransitionsForLatin would have not inserted any code at that point as both those blocks are listed as part of the latin block, but apparently not.... – David Carlisle May 24 '19 at 6:56

ucharclasses has problems if at a boundary there is both: an in and an out insert. It then uses only one, and which one even depends on the order of the declaration. This means that as long as there are word spaces between different blocks it works okay, but as soon as the in and out-code is in one word you get problems. As Latin is a combination of blocks this lead to your example: there are chars from two blocks in one word.

So when setting up transitions you have to keep this in mind. In the case where you have to combine chars from two block in one word, you can try to add a zero-width char to separate them:

\documentclass{article}
\usepackage{fontspec}
\usepackage[]{ucharclasses}
\newfontfamily\latinfont{Arial}
\newfontfamily\unicodefont{Times New Roman}

\begin{document}

\setTransitionsFor{BasicLatin}{\latinfont}{\unicodefont}
\setTransitionsFor{LatinSupplement}{\latinfont}{\unicodefont}
\setTransitionsFor{GreekAndCoptic}{\unicodefont}{\latinfont}

Doppelgänger αβ-Doppel Doppel-αβ

\setTransitionsFor{LatinSupplement}{\latinfont}{\unicodefont}
\setTransitionsFor{BasicLatin}{\latinfont}{\unicodefont}
\setTransitionsFor{GreekAndCoptic}{\unicodefont}{\latinfont}

Doppelgänger αβ-Doppel Doppel-αβ

\setTransitionsFor{Latin}{\latinfont}{}
\setTransitionsFor{BasicLatin}{\latinfont}{}
\setTransitionsFor{GreekAndCoptic}{\unicodefont}{}

Doppelgänger αβ-Doppel Doppel-αβ αβ-^^^^200dDoppel

\setTransitionsFor{GreekAndCoptic}{\unicodefont}{}
\setTransitionsFor{Latin}{\latinfont}{}
\setTransitionsFor{BasicLatin}{\latinfont}{}

Doppelgänger αβ-Doppel Doppel-αβ Doppel-^^^^200dαβ
\end{document}


• It then uses only one, and which one even depends on the order of the declaration. Oh, wow, that didn't cross my mind. – AndreKR May 24 '19 at 9:51