Wednesday, January 13, 2010

TeXing Chinese, Japanese, Korean (UTF-8) Documents using xeLateX

I have been converted myself from a Word user to a TeX user for over 5 years.  Since the release of xeTeX (a new TeX package that can use system fonts to type Non-Egnlish [UTF-8] documents), I have been using it whenever I wanted to type a Chinese document.  However, it is not easy.  The biggest hurdle is that you have to write some commands in the preamble and have special packaged fonts to be able to switch between Chinese and English if you want to use beautiful TeX Fonts in your documents.  

Here is the font package provided by Edward Lee (LGJ).  The manual of the way to use it is here.

Nonetheless, last year, Sun Wenchang released a TeX package XeCJK . It makes TeXing with UTF-8 documents easier.  You can specify the use of Chinese fonts in the document without changing the default English font (Latin Modern).


Here is the template of how things get done:


\documentclass{article}
\usepackage{fontspec} % package for setting fonts
\usepackage{xeCJK}    % package for setting Chinese fonts
\defaultfontfeatures{Mapping=tex-text}
% Chinese font main settings
\setCJKmainfont[BoldFont={Microsoft JhengHei},ItalicFont={DFKai-SB}]{PMingLiU} 
\setCJKsansfont{cwTeXYen}
\setCJKmonofont{Adobe Fangsong Std}  % or cwTeXFangSongTT
% linebreak setting for Chinese
\XeTeXlinebreaklocale "zh"          
\XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt 

\setCJKfamilyfont{zhhei}{Microsoft JhengHei} % or cwTeXHeiBold
\setCJKfamilyfont{zhkai}{DFKai-SB} % or cwTeXKaiItalic
\setCJKfamilyfont{zhsong}{Adobe Song Std}
\setCJKfamilyfont{zhfs}{Adobe Fangsong Std} % or cwTeXFangSongTT
\setCJKfamilyfont{zhyou}{cwTeXYen}
\newcommand*{\song}{\CJKfamily{zhsong}} 
\newcommand*{\fs}{\CJKfamily{zhfs}} 
\newcommand*{\hei}{\CJKfamily{zhhei}}  
\newcommand*{\kai}{\CJKfamily{zhkai}} 
\newcommand*{\yen}{\CJKfamily{zhyou}}
\title{\it 文件題目}
\author{作者}
\date{\today}
\begin{document}
\maketitle
This is a Chinese document. 這是一份中文文件。\textbf{This is a Chinese document. 這是一份中文文件。}\textit{This is a Chinese document. 這是一份中文文件。}{\song This is a Chinese document. 這是一份中文文件。}{\fs This is a Chinese document. 這是一份中文文件。}{\hei This is a Chinese document. 這是一份中文文件。}{\kai This is a Chinese document. 這是一份中文文件。}{\yen This is a Chinese document. 這是一份中文文件。}
\end{document}



Still, you might need some effort to find out the fontname in your system in order to make it working.  For instance, you can use 標楷體 as the Kai font in the template.  Or to be more general, use DFKai-SB, which is a default English name for this font.  If you download the fonts provided by LGJ, the fontnames are commented in the template.

There is one the last thing to make things working.  You have to pick a TeX editor that can edit UTF-8 documents.  I found TeXmaker or Texwork are two good ones.

2 comments:

Tal Galili said...

Hello there Yu,
I wished to contact you via E-mail but wasn't able to find it on the blog.

I was wondering if you might want to join the R bloggers network R-bloggers, through here:
http://www.r-bloggers.com/add-your-blog/

More about it here:
http://www.r-bloggers.com/about/

All the best!
Tal

environmental politics said...

Hi Yu-Sung,
I am following your blog on Twitter. Look forward to reading your posts.

Also, when you have a chance check out my blog http://environmentalpolitics.wordpress.com. I'll link your blog to mine.