Sublime Text 2 with LaTeX & Skim on Mac (XeTeX支援中文)

原文地址:http://tech.marsw.tw/blog/2014/04/18/sublime-text-2-with-latex-skim-on-mac-xelatex-chinese-support/ 作者:(没看到。。)

環境:Mac OS X 10.9.2

Step 1. 安裝 MacTeX

到官網去下載,然後安裝
需要4GB的安裝空間,還有穩定的網路(2.36GB要下載)
這段時間有點久啊@@,這段時間可以先做以下的事情(不影響環境建置)

Step 2. 安裝 Package Control

將以下程式碼複製,在Sublime Text2中,Control+`開啓Colsole貼上

import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installedpackagespath(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.installopener(urllib2.buildopener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'

Sublime Text 2 with LaTeX & Skim on Mac (XeTeX支援中文)_第1张图片
按下Enter執行,之後會顯示'Please restart Sublime Text to finish installation'
就關掉Sublime Text2,然後重新開啟吧!

Step 3. 安裝及設定 LaTeXTools

Command+Shift+P開啟 指令面板(Command Palette),
輸入Install Package(大小寫沒差),應該輸入到一半就有建議的選項跳出,選擇安裝
Sublime Text 2 with LaTeX & Skim on Mac (XeTeX支援中文)_第2张图片
然後會再開啟一個指令面板(Command Palette),
這時再輸入LaTeX Tools(大小寫沒差),也是會有建議選項,選擇就安裝完成啦!

到了這步其實就可以編譯LATEX了,只是接下來繼續講解如何支援中文,還有編譯後自動開啟PDF(STEP 4.)

而MacTeX包含了XeTeX typesetting engine,就不用再額外安裝中文字型,可以直接用系統的中文字型
再來就是要設定支援中文,使用XeTeX中的xelatex progrom來達成我們的目的
修改~/Library/Application Support/Sublime Text 2/Packages/UserLaTeXTools.sublime-settings
builder_settings底下增加這一行"program":"xelatex",

"builder_settings" : {
        "program":"xelatex",
        "display_log" : false,

關掉Sublime Text2,然後重新開啟吧!

Step 4. 安裝及設定 Skim

下載Skim,並安裝後
開啟Skim,在 偏好設定(Preferences)>同步(Sync)中調整設定~
預設:自定(Custom)
指令(Command):/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl
參數不用做更動,就可以把Skim關閉了。
Sublime Text 2 with LaTeX & Skim on Mac (XeTeX支援中文)_第3张图片
Sublime Text 2 with LaTeX & Skim on Mac (XeTeX支援中文)_第4张图片


Demo

等到上述所有程式、Package都安裝完成,
在Sublime Text 2上,Command+N開一個新檔案,把以下這些貼上去:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{BiauKai}   

\title{Title}
\author{Your Name}
 
\begin{document}
 
\maketitle{}
 
\section{Introduction}
 
Testing 中文內容

\end{document}

Command+S存檔成.tex檔(Building System會自動設為LaTeX)
Command+B編譯

Warning可以不要理他xD
成功的話,就會生成pdf,並且自動用Skim開啟pdf預覽!
Sublime Text 2 with LaTeX & Skim on Mac (XeTeX支援中文)_第5张图片

不過同時也產生不少額外files
螢幕快照 2014-04-19 上午1.15.13.png

建議字體採用英文名字,可參考wiki查詢

網路上有很多LaTeX模板可以使用,以後要寫論文、報告、履歷......都可以用LaTeX啦 =D

參考文章

1.Making your first PDF with LaTeX and Sublime Text 2 for Mac
2.Mac上使用Sublime text 2編寫Latex文件
3.部署MAC上的Sublime Text+LaTex中文环境
4.TeX, LaTeX, XeTeX, PdfTeX等等


後記

網路上目前教學在Mac配合LaTeX中文化主要都以參考文章3.,修改LaTeX.sublime-build為主,
LaTeXTools/LaTeX.sublime-build長的跟網路上說的很不一樣,
不設這一步又會直接噴錯:

/usr/local/texlive/2013basic/texmf-dist/tex/latex/fontspec/fontspec.sty:41: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! [ }]

開了log檔,找到最關鍵的地方:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Fatal fontspec error: “cannot-use-pdftex”
!
! The fontspec package requires either XeTeX or LuaTeX to function.
!
! You must change your typesetting engine to, e.g., “xelatex” or “lualatex”
! instead of plain “latex” or “pdflatex”.
!
! See the fontspec documentation for further information.
!
! For immediate help type H .
!………………………………………..

Sublime Text的LaTex要能支援中文
就是在這最關鍵的一行

\usepackage{fontspec}

而這行要能跑,就得把預設的engine改成xelatex

更改Sublime Text的Buile Setting老半天,網路上丟了各種噴的error訊息去google
最後還是去翻了SublimeText在Github的LaTeXTools README才順利解決

  • From now on, LaTeXTools will use a single settings file, called LaTeXTools.sublime-settings, which must exist in the User directory. By this I mean that LaTeXTools will not work until you have a proper LaTeXTools.sublime-settings file in the User directory.
  • The LaTeX.sublime-build file is now for internal use only. Do not modify it! If you have a customized copy inUser, delete it (but do not delete the original in the LaTeXTools directory). See the Settings section below for ways to easily customize the build command.
  • builder-settings: these are builder-specific settings. For the default/traditional builder, the following settings are useful:
    • program: one of pdflatex (the default), xelatex or lualatex. This selects the TeX engine.

所以怎麼更改 LaTeXTools/LaTeX.sublime-build,都沒有用,
因為編譯的時候吃的是Users/LaTeXTools.sublime-settings
除非你要重新LaTeXTools: Reconfigure and migrate settings

而且舊版的格式也不適用了

"cmd": ["latexmk", 
    "-e", "\\$pdflatex = 'pdflatex %O -file-line-error --max-print-line=200 -synctex=1 %S'",
    "-e", "\\$pdf_previewer = qq{if [ -n \"`ps axww | grep Skim | grep -v grep`\" ] ; then osascript -e 'tell app \"Skim\" to revert front document' ; else open -a Skim %O %S ; fi}",
    //"-e", "\\$pdf_previewer = qq{/Applications/Skim.app/Contents/SharedSupport/displayline -g -r 0 %S}",
    "-pv", "-pdf",  "$file"],

你可能感兴趣的:(LaTex,Sublime)