You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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.

Here is a summary logging how I made XeTex worked on Snow Leopard (precisely I used XeLaTex)and hope it can help who visit this page.

  1. Install XeTex.
    XeTex is part of TexLive which is included in MacTex. A full installation of MacTex will have XeTex also installed.
    Since MacTex is very big, some time the installation may failed and you do not know. I encountered a problem telling me that ifpdf.sty could not 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 is no files. By reinstalling the whole MacTex, I solved the problem.
  2. Write yourself a small micro package to set the fonts. save the following in zhfontcfg.sty in the folder where your tex file locate.
    % xelatex 字体设定宏包(xelatex font settings for Chinese)
    % Tested under Mac OS X Snow Leopard(10.6.7) 
    
    \ProvidesPackage{zhfontcfg}
    \usepackage{fontspec,xunicode,xltxtra}
    %\usepackage[EU1]{fontenc}
    \defaultfontfeatures{Mapping=tex-text} %如果没有它,会有一些 tex 特殊字符无法正常使用,比如连字符。
    
    % 中文断行(line break for Chinese)
    \XeTeXlinebreaklocale "zh"
    \XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt
    
    %将系统字体名映射为逻辑字体名称,主要是为了维护的方便
    \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

  3. Edit xelatexChineseTest.tex
    \documentclass{article}
    \usepackage{zhfontcfg}
    \usepackage{times}
    \usepackage[scaled=0.87]{helvet} % better visual match w/ Times
    \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 
    No files shared here yet.
  • No labels