Thursday, June 11, 2015

Write Chinese article in LaTeX using gVim on Windows 7

  1. If you are already editing a tex file, convert it to UTF-8 encoding, then save it, with the following gVim command.

    :set fileencoding=utf8
    :w
     
  2. If you would like gVim to save your file in UTF-8 encoding by default, add the following two lines to the text file"C:\Program Files\vim\_vimrc"

    set encoding=utf-8  " The encoding displayed.
    set fileencoding=utf-8  " The encoding written to file.
     
  3. However, after I added the two lines above, the menu and text area of gVim became gibberish. Thus, I add the following lines to "C:\Program Files\vim\_vimrc" to set the menu and text area to English.

    set langmenu=en_US
    let $LANG = 'en_US'
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vi
     
  4. If you use VIM-Latex to compile your tex file, you have to make VIM-Latex to compile with XeLaTeX rather than pdfLaTeX. Add the following line to "C:\Program Files\vim\_vimrc"

    let g:Tex_CompileRule_pdf='xelatex --interaction=nonstopmode $*'
     
  5. Add the following lines to the preamble of your tex file, after \documentclass.

    \usepackage{fontspec} %加這個就可以設定字體
    \usepackage{xeCJK} % 讓中英文字體分開設置
    \setCJKmainfont{標楷體} %設定中文為系統上的字型,而英文不去更動,使用原TeX字型
    \XeTeXlinebreaklocale "zh"
    \XeTeXlinebreakskip = 0pt plus 1pt %這兩行一定要加,中文才能自動換行
     
  6. However, on my machine, XeLaTeX gave me the following error message.

    fontspec error: "font-not-found" ! ! The font "????" cannot be found.

    Thus, I changed this line above

    \setCJKmainfont{標楷體}

    to

    \setCJKmainfont{DFKai-SB}

    It won't let me use Chinese font names, strange!

  7. If you want to use 新細明體, try the following line instead.

    \setCJKmainfont{PMingLiU}

    If you want to use 微軟正黑體, try the following line instead.

    \setCJKmainfont{Microsoft JhengHei}

Reference

Thanks to all the answerers on StackExchange.
LaTeX tutorial by Academia Sinica (in Chinese)

No comments:

Post a Comment