Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In a XeLaTeX document whose main language is non-Latin (in the sense of using non-Latin alphabet), I'd like the Latin text to be typeset with a different font family from that of the main text. How do I do that in an automatic, document-wide manner?

For example, suppose that the main language is Thai and the XeLaTeX source is as follows:

\documentclass{article}

\usepackage{polyglossia}
\usepackage{fontspec}
\setdefaultlanguage{thai}
\newfontfamily\thaifont{Thonburi}

\begin{document}
ดาวพุธมีหลุมอุกกาบาตจำนวนมากจนดูคล้ายดวงจันทร์ ภูมิลักษณ์ที่เด่นที่สุดบนดาวพุธ คือ แอ่งแคลอริส (Caloris Basin)
\end{document}

I'd like the phrase 'Caloris Basin' to be typeset with Cambria instead of Thonburi. Trial and error doesn't lead me to the desired result so far.

share|improve this question
add comment

3 Answers 3

up vote 7 down vote accepted

Just set the font for the other language like you do for the main language, and then use \text<otherlang>{...} for small portions of that language or \begin{<otherlang>} ... \end{<otherlang>} for larger portions:

% !TEX TS-program = XeLaTeX

\documentclass{article}

\usepackage{polyglossia}
\usepackage{fontspec}
\setdefaultlanguage{thai}
\setotherlanguage{english}
\newfontfamily\englishfont{Cambria}
\newfontfamily\thaifont{Thonburi}

\begin{document}
ดาวพุธมีหลุมอุกกาบาตจำนวนมากจนดูคล้ายดวงจันทร์ ภูมิลักษณ์ที่เด่นที่สุดบนดาวพุธ คือ 
แอ่งแคลอริส \textenglish{(Caloris Basin)}
\end{document}

output of code

Comment

If your question was asking whether it's possible to detect the language automatically from the source, the answer is, possibly (see the other answers) although these methods add some complication. The safest route is to use some markup to distinguish the different languages.

share|improve this answer
    
What about text entered without \englishtext, what font will it assume? I ask this because sometimes when I have one English word I do not need to use /englishtext and just write it directly, and I see that it does not have the same font...so how to specify its font? –  student1 Jun 8 at 23:45
    
As mentioned in my comment, if you want to do this without markup, you can use the method mentioned in the other answers (specifically egreg's answer, which is the most straightforward way to implement it). –  Alan Munn Jun 9 at 0:45
add comment

XeTeX's interchartoks feature can be used in a limited sense for this sort of thing. An example is shown below.

With this feature, each character is assigned a "char class", and if the char class switches (say from Thai to Latin) from one character to the next in the source, additional material can be inserted. In the example, \thaifont is inserted when the char class switches from default to Thai, and \otherfont when the char class switches from Thai to the default.

There are edge cases that can prove tricky with this feature, but if you're just interested in small snippets within largely Thai text I believe this feature can be quite handy.

\documentclass{article}

\usepackage{polyglossia}
\usepackage{fontspec}
\setdefaultlanguage{thai}
\newfontfamily\otherfont{Times}
\newfontfamily\thaifont{Thonburi}

\XeTeXinterchartokenstate = 1\relax
\newXeTeXintercharclass\thaiclass
\ExplSyntaxOn
\int_step_inline:nnnn {"0E00}{1}{"0E7F}
 { \XeTeXcharclass #1 = \thaiclass }
\clist_map_inline:nn {\(,\),\.,\,,\:,\;,\?,\!}
 { \XeTeXcharclass `#1 = \thaiclass\relax }
\ExplSyntaxOff
\XeTeXinterchartoks \thaiclass 0 = {\otherfont}
\XeTeXinterchartoks 0 \thaiclass = {\thaifont}

\begin{document}
ดาวพุธมีหลุมอุกกาบาตจำนวนมากจนดูคล้ายดวงจันทร์ ภูมิลักษณ์ที่เด่นที่สุดบนดาวพุธ คือ แอ่งแคลอริส (Caloris Basin)
\end{document}

Update: it's quite essential to use the Thai font for all punctuation as well, since the char class feature seems to behave somewhat unexpectedly with spaces.

share|improve this answer
    
Hi Will, I didn't add this kind of solution to my answer because of the potential problems. Won't this mess up with things like numerals and punctuation? Will they come from the Thai font or the other font? –  Alan Munn Jun 6 at 4:36
    
@AlanMunn — To be honest I haven't used this feature much in practise. The punctuation by default belongs to class 0, so if you want it to be from the Thai font (in fact I think this is necessary), you'd need to insert some more \XeTeXcharclass lines. –  Will Robertson Jun 6 at 4:43
    
Which is what ucharclasses does. –  egreg Jun 6 at 7:20
    
@AlanMunn: Package like xecjk uses this feature quite a lot (but sometimes punctuation are problematic as you can find by searching a bit). –  Ulrike Fischer Jun 6 at 8:50
    
@egreg — Ah, I should have remembered about this package; apologies. –  Will Robertson Jun 7 at 14:57
add comment

The package ucharclasses does this. For short insertions of text in other alphabets hyphenation shouldn't be a concern; for longer parts, using the various features of polyglossia is recommended.

\documentclass{article}

\usepackage{polyglossia}
\usepackage{fontspec}
\setmainlanguage{thai}

\newfontfamily\thaifont{Thonburi}
\newfontfamily\englishfont{TeX Gyre Bonum} % just to get a distinctive font

\usepackage[Latin,Thai]{ucharclasses}

\setTransitionsForLatin
  {\begingroup\englishfont}
  {\endgroup}

\begin{document}

ดาวพุธมีหลุมอุกกาบาตจำนวนมากจนดูคล้ายดวงจันทร์ ภูมิลักษณ์ที่เด่นที่สุดบนดาวพุธ คือ
แอ่งแคลอริส (Caloris Basin) ดาวพุธมีหลุมอุกกาบาตจำนวนมากจนดูคล้ายดวงจันทร์
ภูมิลักษณ์ที่เด่นที่สุดบนดาวพุธ คือ แอ่งแคลอริส

\end{document}

enter image description here

Note: the code produces an overfull box, due to the very long words; as I don't know Thai, I just copied back the same text.

share|improve this answer
1  
I think this is the correct answer. –  Will Robertson Jun 7 at 14:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.