- Published on
An Introduction to LaTeX for Typesetting Thai Documents
- Authors
- Name
- Tanyaruek Boontharaksa
- @mkzgin
This is a guide to typesetting documents in Thai using LaTeX, a free software for document preparation. There are many guides to LaTeX out there, but many do not involve typesetting Thai language. Although you can find many templates on the internet, almost none describe what the codes in the document preamble section are doing, leaving you confused. Luckily there is a document written by Dittaya Wanvarie from Chulalongkorn University that described almost every line of the preamble section needed to typesetting in Thai, but maybe it is too brief and some codes do not work correctly. Being inspired by Dittaya's work and Ittipat's GitHub LaTeX repository, here I present “my” version of the document preamble and try to provide details on how each line works. The full code will be provided at the end of this post.
Requirements
You can use any TeX environments that support these packages: fontspec
, polyglossia
, ucharclasses
, and setspace
. We expected that your environment would have CMU Serif and Sarabun installed already. We use XeLaTeX as a compiler for our document, as it supports UTF-8 encoded Unicode text.
The Preamble
First comes the XeTeX
settings:
\XeTeXlinebreaklocale "th"
\XeTeXlinebreakskip = 0pt plus 0pt
This sets the locale to Thai and tells the program not to break between words. The \XeTeXlinebreaklocale
receives arguments in the ISO 639-1 language codes, according to this TeX - LaTeX Stack Exchange question and the 0pt plus 0pt
is a LaTeX glue, which is the concept well-described in the article by Nelson H. F. Beebe. The reference documentation of XeTeX can be found here. Furthermore, the use of \sloppy
helps with text justification, and we decided to use it as you can see in the final code.
The next three lines of code involve the package fontspec
.
\usepackage{fontspec}
\newfontfamily{\defaultfont}[Mapping=tex-text]{CMU Serif}
\newfontfamily{\thaifont}[Scale=MatchLowercase,Mapping=tex-text,UprightFont={* Light}]{Sarabun}
The first line, as implied by what is written, calls the package. The second line sets the default font, which is for Latin alphabets, CMU Serif. The reason for this is that the Sarabun font, which is a Thai font defined in line 3, is a sans-serif font. This sans-serif font may be hard to read for some people, so we opted for CMU Serif. The option Mapping=tex-text
forms the usual ligatures in the document (such as the proper orientation of quotation marks). The option Scale=MatchLowercase
scales the font being selected to match the current default Roman font to the height of the lowercase. Lastly, the option UprightFont={* Light}
sets the font weight to light1. The fontspec
package documentation can be found here.
Next, we look at the package polyglossia
.
\usepackage{polyglossia}
\setdefaultlanguage{thai}
\setotherlanguages{english}
This is an alternative to the babel
package for tasks described in the documentation. For example, if you set the default language to Thai, the date format will be in Thai.
Here we differ from Dittaya's document, instead of defining the new environment thailang
, we use the following:
\usepackage[Latin,Thai]{ucharclasses}
\setDefaultTransitions{\defaultfont}{}
\setTransitionTo{Thai}{\thaifont}
The package ucharclasses
helps us deal with changing font while switching between Thai and English. The use of this package can be seen in the documentation. As Piyapong pointed out in his reply to a question, what is done in Dittaya's document causes a problem of starting a new line after changing language or after the beginning of a mathematics block.
Last comes the setspace
package, which is used to set line spacing here.
\usepackage[onehalfspacing]{setspace}
We will not discuss line spacing here, because it is rather complicated to the extent that different word processors interpret the meaning of "one-half spacing" in different ways. If you are interested in how these things work, you can read this article on Overleaf. Even more, if you are into typography, you can read the related section from the online book "Butterick's Practical Typography".
The completed code is provided here.
\XeTeXlinebreaklocale "th"
\XeTeXlinebreakskip = 0pt plus 0pt
\usepackage{fontspec}
\newfontfamily{\defaultfont}[Mapping=tex-text]{CMU Serif}
\newfontfamily{\thaifont}[Scale=MatchLowercase,Mapping=tex-text,UprightFont={* Light}]{Sarabun}
\usepackage{polyglossia}
\setdefaultlanguage{thai}
\setotherlanguages{english}
\usepackage[Latin,Thai]{ucharclasses}
\setDefaultTransitions{\defaultfont}{}
\setTransitionTo{Thai}{\thaifont}
\usepackage[onehalfspacing]{setspace}
\sloppy
Keep in mind that this is the minimal code in the preamble that makes it work, despite not being the best practice in some sense2. To use the code provided in your document, do not forget to include \documentclass
and the beginning and end of the document tags. Other packages involving mathematics typesetting and page setup are not discussed here because there are many useful guides online such as on the Overleaf website.
Afterthought
At first, I decided not to point out other resources based on my assumption that all the readers could search for the information themselves. But after some time I realized that putting a list of resources at the end of the post might shorten the time the readers need to serve the internet. And here we are.
- Dittaya Wanvarie's LaTeX for Typesetting Academic Documents is a good resource in Thai for beginners.
- Tobias Oetiker's The Not So Short Introduction to LaTeX is fun to read. This covers almost every aspect of day-to-day typesetting in LaTeX.
- The Overleaf website needs to be mentioned twice in this post because of its readability for LaTeX learners and usefulness as a reference.
- The LaTeX Companion, third edition is out now. This two-volume book is good for reference, not for beginners though.
Footnotes
This is not required if you choose TH Sarabun New as your main font because the default weight is lighter than of Sarabun. I chose Sarabun because it is optimized after TH Sarabun New. ↩
The use of the package
ucharclasses
can cause some problems, as the package only 'just works' for Unicode blocks as described on the second page of the documentation. ↩