Making LaTex support CJK (Chinese, Japanese, Korean) characters is always a pain process. When I used Ubuntu, I used CJK LaTex package. It needs to compile and install fonts (e.g. cyberbit) which is not a straightforward task. When I migrate to Mac OS X, I found a lot people recommend XeTex to deal with CJK documents which does not need to install fonts, instead it uses system fonts.

Here is a summary log about how I made XeTex work on Snow Leopard (precisely I used XeLaTex). I hope this can help the one who also need to use CJK characters in LaTex under Mac OS X.

  1. Install XeTex.
    XeTex is part of TexLive which is included in MacTex. A full installation of MacTex will have XeTex installed.
    Since MacTex is very big, sometimes the installation may failed and you do not know. I encountered a problem telling me that ifpdf.sty could not be found. I googled and found it belongs to oberdiek package. In the Tex Live Utility(Under Application->Tex folder) it shows oberdiek is installed but in detail info there are no files. By reinstalling the whole MacTex, I solved this problem.
  2. Write a small micro package to set the fonts. Save the following in zhfontcfg.sty in the folder where your tex files located.
    % xelatex 字体设定宏包(xelatex font settings for Chinese)
    % Tested under Mac OS X Snow Leopard(10.6.7) 
    
    \ProvidesPackage{zhfontcfg}
    \usepackage{fontspec,xunicode,xltxtra}
    \defaultfontfeatures{Mapping=tex-text} %如果没有它,会有一些 tex 特殊字符无法正常使用,比如连字符。
    
    % 中文断行(line break for Chinese)
    \XeTeXlinebreaklocale "zh"
    \XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt
    
    %将系统字体名映射为逻辑字体名称,主要是为了维护的方便
    % You can use fc-list command to find what font are available in your system.
    \newcommand\fontnamehei{Heiti SC}
    \newcommand\fontnamesong{STSong}
    \newcommand\fontnamekai{STKaiti}
    \newcommand\fontnamemono{Menlo}
    \newcommand\fontnameroman{Times}
    
    
    \setmainfont{\fontnameroman} % set default font
    %\setmainfont[BoldFont=\fontnamehei]{\fontnamesong} %如果正文以中文为主,设置文档正文字体为宋体
    %\setsansfont[BoldFont=\fontnamehei]{\fontnamekai}
    %\setmonofont{\fontnamemono}
    
    %宋体
    \newfontfamily\SONG{\fontnamesong}
    \newcommand{\song}[1]{{\SONG #1}}
    
    %楷体
    \newfontfamily\KAI{\fontnamekai}
    \newcommand{\kai}[1]{{\KAI #1}}
    
    %黑体
    \newfontfamily\HEI{\fontnamehei}
    \newcommand{\hei}[1]{{\HEI #1}}
    
    %英文
    \newfontfamily\ENF{\fontnameroman}
    \newcommand{\en}[1]{\,{\ENF #1}\,}
    \newcommand{\EN}{\,\ENF\,}
    

    newfontinstance is deprecated

    Some posts on the Internet use \newfontinstance which is deprecated and will cause error; please use \newfontfamily or \newfontface instead.

    Compatibility

    The package times or mathptmx will overwrite settings like \setmainfont in the zhfontcfg.sty, make sure load zhfontcfg after loading those packages.

    Use correct fonts in your system

    Use

    fc-list

    in shell to check what fonts are installed in your system and use them instead of the fonts I listed above("Heiti SC","STSong", and so on).

  3. Edit xelatexChineseTest.tex
    \documentclass{article}
    \usepackage{zhfontcfg}
    \begin{document}
    
    Test:\\
    Hello world! 你好!(Default font)\\
    \song{Hello world! 你好!(宋体,Song)}\\
    \kai{Hello world! 你好!(楷体,Kai)}\\
    \hei{Hello world! 你好!(黑体,Hei)}\\
    \en{Hello world! (Roman)}\\
    
    \end{document}
    
  4. Compile the tex file and see results.
     xelatex xelatexChineseTest.tex 
      File Modified
    File zhfontcfg.sty May 28, 2011 by Anhei Shu
    File xelatexChineseTest.tex May 28, 2011 by Anhei Shu
  • No labels