0

How can I use ucharclasses with babel in XeLaTeX while defining the font with \babelfont? Alternatively, is there a method to achieve automatic locale and font switching in XeLaTeX, rather than LuaLaTeX, without relying on ucharclasses? I prefer not to replace babel with polyglossia, and I've seen this method on Stack Exchange before. Here's a minimal working example of my attempt.

\documentclass{article}

\usepackage{babel}
\usepackage[CJK,Cyrillics,Latin]{ucharclasses}

\babelprovide[import]{chinese}
\babelprovide[import,main]{english}
\babelprovide[import]{french}
\babelprovide[import]{russian}

\babelfont{rm}[
    Extension       = .otf,
    BoldFont        = NewCM10-Bold,
    ItalicFont      = NewCM10-BookItalic,
    BoldItalicFont  = NewCM10-BoldItalic
]{NewCM10-Book}
\babelfont{sf}[
    Extension       = .otf,
    BoldFont        = NewCMSans10-Bold,
    ItalicFont      = NewCMSans10-BookOblique,
    BoldItalicFont  = NewCMSans10-BoldOblique
]{NewCMSans10-Book}
\babelfont{tt}[
    Extension       = .otf,
    BoldFont        = NewCMMono10-Bold,
    ItalicFont      = NewCMMono10-BookItalic,
    BoldItalicFont  = NewCMMono10-BoldOblique
]{NewCMMono10-Book}
\babelfont[chinese]{rm}[
    Extension       = .otf,
    BoldFont        = FandolSong-Bold,
    ItalicFont      = FandolKai-Regular
]{FandolSong-Regular}
\babelfont[chinese]{sf}[
    Extension       = .otf,
    BoldFont        = FandolHei-Bold
]{FandolHei-Regular}
\babelfont[chinese]{tt}[
    Extension       = .otf
]{FandolFang-Regular}

\begin{document}

This is English.

这是中文。

Это русский.

\end{document}

I'd appreciate any insights on how to get ucharclasses to work with babel in XeLaTeX, or suggestions for alternative approaches to achieving automatic font and locale switching. Thanks in advance!

2 Answers 2

2

You must define the transitions. An option is:

\setTransitionsForCyrillics{\selectlanguage{russian}}{\selectlanguage{english}}
\setTransitionsForChinese{\selectlanguage{chinese}}{\selectlanguage{english}}

However, ucharclasses doesn’t include the Chinese period in the Chinese text.

4
  • Thank you very much. But could you please explain why the individual Chinese periods are printed correctly? Commented May 9 at 17:26
  • No idea, to be honest. Either it’s a limitation of ucharclasses or it’s a limitation of mine. Commented May 9 at 17:45
  • Wouldn't \begin{otherlanguage*} and \end{otherlanguage*} be better?
    – egreg
    Commented May 9 at 21:19
  • I don’t like opening a group and trusting it’s closed. It’s a matter of personal taste, I’d say. Commented May 10 at 17:29
0

Using the command \selectlanguage or the environment otherlanguage* are good approaches, but the following method seems to work better. For example, consider 你好,\languagename\languagename.

\newif\iflang@save
\newif\iflang@set

\setTransitionsForCJK{%
    \iflang@save
        \iflang@set
            \edef\langname{\languagename}%
        \fi
    \else
        \edef\langname{\languagename}%
    \fi
    \selectlanguage{chinese}\lang@savetrue\lang@setfalse%
}{%
    \edef\selectlang{\noexpand\selectlanguage{\langname}}\selectlang\lang@savefalse\lang@settrue%
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .