身為一個 vimer ,想要在 vim 上面寫 latex 也是非常合理的事。今天中午和下午,花了一點時間把 vim + latex 的環境配置好,所以趕緊補了這一篇來記錄一下怎麼配置。
示範影片
vim-latex
首先,我們會需要一個快速編寫 latex 的 plugin,叫做 vim-latex。如果使用 Vundle 的話,那麼安裝方法很簡單,只要在 .vimrc 加上這幾行,然後 :PluginInstall 即可。
Plugin 'vim-latex/vim-latex' "-----------------latex------------------------- " REQUIRED. This makes vim invoke Latex-Suite when you open a tex file. filetype plugin on " IMPORTANT: win32 users will need to have 'shellslash' set so that latex " can be called correctly. set shellslash " IMPORTANT: grep will sometimes skip displaying the file name if you " search in a singe file. This will confuse Latex-Suite. Set your grep " program to always generate a file-name. set grepprg=grep\ -nH\ $* " OPTIONAL: This enables automatic indentation as you type. filetype indent on
接著,如果像我是在 mac 上面使用 vim,那麼 alt 可能沒有辦法正確編寫在 vimrc 裡面,那麼一些快速鍵就要用這個方式來編寫。
" OPTIONAL: Starting with Vim 7, the filetype of empty .tex files defaults to " 'plaintex' instead of 'tex', which results in vim-latex not being loaded. " The following changes the default filetype back to 'tex': let g:tex_flavor='latex' autocmd filetype tex imap ∫ Tex_MathBF autocmd filetype tex imap ç Tex_MathCal autocmd filetype tex imap ¬ Tex_LeftRight autocmd filetype tex imap ˆ Tex_InsertItemOnThisLine
vim-latex 的教學影片可以參照這個連結。
vim-latex-live-preview
接著要配置第二頁 Plugin,叫做 vim-latex-live-preview。為什麼要配置這個呢,因為我想要即時編譯 latex,然後看我寫出來的 latex 長的如何,也可以把它想像成網頁的 hot-reload。因此要加上這段程式碼:
Plugin 'xuhdev/vim-latex-live-preview'
然後個人設定的部分如下:
"-----------------latex live preview------------------------- autocmd filetype tex setl updatetime=1000 let g:livepreview_previewer = 'open -a Skim'
updatetime 的意思是多久做一次 hotreload,這部分建議值不要太小,不然很吃 CPU。而 previewer 的部分,可以選擇自己喜歡的,我選擇的是 Skim 這個程式,請至連結下載安裝。
接著請在 skim 的 preference 修改同步設定如下:
hot reload 就完成了。
接著我還有一些比較私人的設定如下,是設定每次開啟 tex 檔時,就自動開 Skim。最後,將 F6 設定為開 Skim。
autocmd filetype tex :LLPStartPreview nmap <F6> :LLPStartPreview<CR> imap <F6> <ESC>:LLPStartPreview<CR>
Latex Cheat Sheet
最後,為了怕自己忘掉一些冷門語法,加入一段 script 來開啟 latex 和 latex-suite cheat sheet (1) (2) (3),如下:
func! LatexHelp() exec "!open -a Preview /path/to/vim-latex.pdf" endfunc
大功告成!
關於我:
我是沒一村,專長和興趣是程式、主動投資、科技商業模式。可以參考我的書單和比較熱門的文章:
您好。請問有何簡單手把手介紹如何在MAC上用vim編寫latex嗎?我是學文科的,看到好多CS術語有點頭大。。。