3

I want to write a paragraph with two languages, Khmer and English. And I want to change font sometimes. Here is the code:

\documentclass{article}
\usepackage[no-math]{fontspec}
\setmainfont[Script=Khmer]{Khmer OS}
\setsansfont[Script=Khmer]{Khmer OS}
\setmonofont[Script=Khmer]{Khmer OS}
\usepackage{ucharclasses}
\setTransitionsForLatin{\fontspec{Latin Modern Roman}}{}
\begin{document}
    អត្ថបទជាភាសាខ្មែរ English text {\fontspec{Lucida Calligraphy} English text in Lucida Calligraphy}
\end{document}

It does not change to Lucida Calligraphy as I expected. What is the matter and how can I fix it?

Say OL
  • 1,793
  • 1
    I'm afraid I don't have the Khmer OS font family on my system, so this is just a comment, not an answer. Instead of using the low-level command \fontspec to execute font changes, have you considered issuing the directive \newfontfamily{\lm}{Latin Modern Roman} in the preamble and then typing something like \title{\lm Hello World}? – Mico Jan 06 '15 at 18:46
  • I think this is a duplicate of http://tex.stackexchange.com/questions/188038/ucharclasses-package-does-not-work-with-tex-live-2014s-fontspec – egreg Jan 06 '15 at 18:58
  • @Mico Actually, when I write English, it automatically typeset in Latin Modern Roman by the command \setTransitionsForLatin{\fontspec{Latin Modern Roman}}{}. But problem is that when I want to change English font to Lucida Calligraphy, it does not work! It still be in Latin Modern Roman – Say OL Jan 06 '15 at 18:59
  • @egreg I think the problem is a bit different! All what I want is that set Khmer OS as my default Khmer font and Latin Modern Roman as my default English font so that I can change from English to Khmer and from Khmer to English without using any command.And I have done with ucharclasses package using command \setTransitionsForLatin... above.The solution in link you suggested is not applicable.If use \newfontfamily\englishfont{Latin Modern Roman}, \setTransitionTo{Latin}{\englishfont} and \setTransitionFrom{Latin}{\normalfont},the default Englis font is also changed. – Say OL Jan 06 '15 at 20:08

2 Answers2

7

You have to set the transitions correctly and never use \fontspec in the document body.

\documentclass{article}
\usepackage[no-math]{fontspec}
\usepackage{ucharclasses}

\setmainfont{Khmer MN} % the only one I have supporting Khmer
\newfontfamily{\lmodern}{Latin Modern Roman}
\newfontfamily{\lucidacal}{Lucida Calligraphy OT}[Scale=MatchUppercase]

\setTransitionTo{Khmer}{\normalfont}
\setTransitionFrom{Khmer}{\lmodern}

\begin{document}
    អត្ថបទជាភាសាខ្មែរ English {\lucidacal English text in Lucida
      Calligraphy} and again Latin Modern

\end{document}

enter image description here

The rendering for Khmer is not good, but I know nothing about the language.

More complex setups are possible; here's an example.

\documentclass{article}
\usepackage[no-math]{fontspec}
\usepackage{ucharclasses}
\usepackage{etoolbox}

\setmainfont{Khmer Sangam MN}
\newfontfamily{\khmeraltfam}{Code2000}[Script=Khmer]
\newfontfamily{\lmodernfam}{Latin Modern Roman}
\newfontfamily{\lucidacalfam}{Lucida Calligraphy OT}[Scale=MatchUppercase]

\newrobustcmd{\lmodern}{\lmodernfam\let\currentenglish\lmodernfam}
\newrobustcmd{\lucidacal}{\lucidacalfam\let\currentenglish \lucidacalfam}
\newrobustcmd{\khmernormal}{\normalfont\let\currentkhmer\normalfont}
\newrobustcmd{\khmeralt}{\khmeraltfam\let\currentkhmer\khmeraltfam}

\lmodern \khmernormal % initialize

\setTransitionTo{Khmer}{\currentkhmer}
\setTransitionFrom{Khmer}{\currentenglish}

\begin{document}
អត្ថបទជាភាសាខ្មែរ English {\lucidacal English text in Lucida
អត្ថបទជាភាសាខ្មែរ
Calligraphy} and again Latin Modern

{\khmeralt អត្ថបទជាភាសាខ្មែរ}

\end{document}

enter image description here

egreg
  • 1,121,712
  • That is great!!! For rendering we just tell fontspec to follow Khmer script rule. \setmainfont[Script=Khmer]{Khmer MN}. Thank @egreg :) – Say OL Jan 07 '15 at 02:44
  • 1
    Oh, I have just tested it last moment. I found it affect to Khmer font changing. If want to use other Khmer font, it has no effect. What I have tested is adding \newfontfamily{\bokor}[Script=Khmer]{Khmer OS Bokor}. When I try \bokor{អត្ថបទជាភាសាខ្មែរ} or {\bokor អត្ថបទជាភាសាខ្មែរ} or \begin{bokor}អត្ថបទជាភាសាខ្មែរ\end{bokor}, it typesets in Khmer OS, the main font. It does not typeset in Khmer OS Bokor. What should I do? Khmer fonts pack – Say OL Jan 07 '15 at 03:33
  • @Say OL, Manual font switching works as expected; automatic font switching works as expected; manual and automatic font switching together also work as expected: when the Khmer run of characters is encountered, the font switches to the automatic one. This happens after the manual font command. Manually set the automatic font (currentkhmer) to Bokor. Enclose in braces to keep the effect local. – Cicada Sep 03 '19 at 11:28
1

MWE for Bokor

For completeness, and using different fonts to illustrate:

Manual font switching works as expected; automatic font switching works as expected; manual and automatic font switching together also work as expected: when the Khmer run of characters is encountered, the font switches to the automatic font. This happens after the manual font command is processed. Manually set the automatic font (currentkhmer) to Bokor. Enclose in braces to keep the effect local.

set automatic font example

\documentclass[12pt]{article}
\usepackage[no-math]{fontspec}
\usepackage{ucharclasses}
\usepackage{etoolbox}
\usepackage{xcolor}

\setmainfont[Script=Khmer]{AKbalthom KhmerHand}
\newfontfamily{\khmeraltfam}{Angkor Sovann Preah Vihear}[Script=Khmer]
\newfontfamily{\lmodernfam}{Noto Serif}
\newfontfamily{\lucidacalfam}{Lucida Calligraphy}[Scale=MatchUppercase]
\newfontfamily\bokor{Bokor}

\newrobustcmd{\lmodern}{\lmodernfam\let\currentenglish\lmodernfam}
\newrobustcmd{\lucidacal}{\lucidacalfam\let\currentenglish \lucidacalfam}
\newrobustcmd{\khmernormal}{\normalfont\let\currentkhmer\normalfont}
\newrobustcmd{\khmeralt}{\khmeraltfam\let\currentkhmer\khmeraltfam}

\lmodern \khmernormal % initialize

\setTransitionTo{Khmer}{\currentkhmer}
\setTransitionFrom{Khmer}{\currentenglish}

\begin{document}
\large
អត្ថបទជាភាសាខ្មែរ English {\lucidacal English text in Lucida
អត្ថបទជាភាសាខ្មែរ
Calligraphy} and again Latin Modern

khmeralt font (in braces) {\khmeralt អត្ថបទជាភាសាខ្មែរ} -- resets currentkhmer

main text (AKbalthom KhmerHand font)  អត្ថបទជាភាសាខ្មែរ

\textbackslash bokor switch \bokor អត្ថបទជាភាសាខ្មែរ --  no effect

bokor in braces: {\bokor អត្ថបទជាភាសាខ្មែរ} -- no effect

{
\let\currentkhmer\bokor
bokor assigned to currentkhmer អត្ថបទជាភាសាខ្មែរ -- works!
}

main text again   អត្ថបទជាភាសាខ្មែរ

red អត្ថបទជា{\color{red}ភាសា}ខ្មែរ more text

\end{document}
Cicada
  • 10,129