Emacs+$\LaTeX$ 幫你寫數學文章


在這個混亂的星球上,有兩大神器.一個是Emacs,另一個是……Vim?No.對學數學的人來說,另一大神器是$\LaTeX$.學數學的朋友對於后者可能比較熟悉,$\LaTeX$是計算機界的大牛,圖靈獎得主高德納的力作,是寫數學論文的不二選擇(嚴格地說高德納發明的是TeX,$\LaTeX$是在TeX的基礎上稍微改造而來的).用$\LaTeX$寫的數學文章,排版優美,數學式子漂亮.在$\LaTeX$面前,Word編輯器,mathtype之類的不值一提.然而,$\LaTeX$不是所見即所得的,也就是說,當你寫數學文章的時候,你面對的,不是一個個數學式子,而是一行行代碼.這樣無疑會讓原本就難看懂的數學式子更難看懂,當你想看看書寫效果的時候,必須編譯一下.如此一來,你的思維流必然會被打斷,你寫文章的士氣必然會降低..而且,當你想輸入一個類似於行間公式的東西的時候,你必須輸入\begin{XXX}...\end{XXX},這無疑會帶給你無窮的麻煩.

 

Emacs很好地解決了$\LaTeX$的這類問題.它和你的計算機上的$\LaTeX$配合起來天衣無縫.Emacs是計算機界的大牛Richard Stallman的力作.它和Tex同屬開源軟件.Emacs估計是很強大的編輯器.我剛剛接觸它.它的優點在於強大的可擴展性和可定制性,不同的人可以把Emacs變成自己想要的樣子,定制出自己想要的功能.用Emacs不僅能寫$\LaTeX$文檔,而且能寫C,C++,....讓大家先睹為快,下面是我用Emacs寫$\LaTeX$文檔時的截圖

 

 

 

你以為我的博文都是我一個個字打出來的么?當然不是,否則我的手早就癱了(不過前兩年真的是這么干的.大一的時候我竟然在使用word寫數學筆記,還得意洋洋地使用mathtype,后來長進了一些,使用winedt和texworks,不過也不高效.直到我遇到了emacs,不過以前,我還在使用Windows操作系統,被emacs的安裝,學習之繁瑣嚇住了,一直沒去用.直到這個暑假,電腦硬盤摔壞之后,我狠了狠心大力改革,徹底讓Windows滾了蛋,安裝了Ubuntu Linux,開始學習使用emacs寫LaTeX文檔.想想以前真可怕.工欲善其事,必先利其器啊.).在Emacs里,我安裝了Auctex插件和cdLaTeX插件,以及Yasnippet.這樣之后,我使用Ctrl+C+E,然后在mini buffer里輸入XXX,就自動出現\begin{XXX}...\end{XXX},在數學環境里,我輸入fr+Tab,就自動出現\frac{}{},而且光標自動出現在第一個{}內.第一個{}寫好了,再Tab一下,光標會自動出現在第二個{}內,第2個括號內的內容寫好了,再Tab一下,光標就跳出括號了.在數學環境里,我輸入sq,Tab一下,\sqrt{}就出來了,而且光標自動在括號內.輸好了,再Tab一下,光標就跳出括號.我輸入matrix,就出來一個下拉菜單,里面讓你選擇到底是輸入一個矩陣呢,還是行列式呢……我輸入Ctrl+C+P+B,就可以在整個buffer里預覽數學公式,輸入Ctrl+C+P+P,就可以再某個特定的點預覽數學公式.諸如此類,帶給人們很多方便,可謂數學論文書寫者的福音.

 

那到底怎么安裝Emacs呢?我認為,在Windows下安裝Emacs稍顯麻煩,所以我轉到了Ubuntu Linux操作系統.首先,既然是寫\LaTeX文檔,ubuntu里當然要先安裝tex系統,最簡單的方法是在terminal里 sudo apt-get install texlive-full ,安裝texlive2009完整版(如果你網速太慢,用apt-get會下載七八個小時的.建議你用在別的windows機器下用迅雷下載texlive2012的iso文件,然后刻盤,再安裝到Linux系統上,然后設置texlive2012的path.我就是這么干的.如果你一定要用apt-get方式下載texlive2009的話,那一定要安裝多線程哦,那樣就可以apt-fast了,下載速度會提高好幾倍的,google一下"多線程  apt-fast"你就知道了).需要下載1G多的東西.這樣就好了.其次,在terminal里sudo apt-get install emacs ,安裝Emacs 編輯器.然后,安裝emacs的LaTeX插件auctex.照目前來看,auctex用apt-get的方式獲得的auctex和ghostscript不協調,會導致auctex 在pdfLaTeX模式下的preview功能失敗,會在preview的時候出現 “紅圈白杠”,紅圈白杠的樣子見 這個鏈接.我當初為此問題困擾了好幾天,google了好長時間才找到解決方案.原來並不是我們的錯.解決方案就是:不要通過apt-get 的方式安裝auctex,要下載 這個auctex包 ,然后將其安裝在ubuntu上,安裝方法參照這個頁面.這樣子,就OK了.

至於emacs的配置,完全要按照你自己的喜好進行了.網上有很多配置的,我的配置文件完全是從網上東拼西湊起來的.如下

View Code
  1 ;為我的emacs配置主題色彩 (color-theme-色彩名稱)  
  2  (add-to-list 'load-path "~/elisp/color-theme")
  3  (require 'color-theme)
  4  (color-theme-initialize)  
  5  (color-theme-oswald)  
  6  
  7  
  8  ;preview-latex
  9  (add-to-list 'load-path
 10  "~/Downloads/auctex-11.86/preview")
 11  (require 'preview)
 12  
 13  ; yasnippet
 14  
 15  
 16  (add-to-list 'load-path
 17           "~/.emacs.d/plugins/yasnippet")
 18  (require 'yasnippet)
 19  (yas/initialize)
 20  (yas/load-directory "~/.emacs.d/plugins/yasnippet/snippets")
 21  (yas/minor-mode-on)
 22  
 23  ; this breaks things; use the below advise solution instead
 24  ;(add-hook 'org-mode-hook
 25  ;       (lambda ()
 26  ;         (org-set-local 'yas/trigger-key [tab])
 27  ;         (define-key yas/keymap [tab] 'yas/next-field-group)))
 28  
 29  ; when cdlatex-mode or org-cdlatex-mode are loaded, we need to change
 30  ; the behaviour of yas/fallback to call cdlatex-tab
 31  
 32  (defun yas/advise-indent-function (function-symbol)
 33    (eval `(defadvice ,function-symbol (around yas/try-expand-first activate)
 34             ,(format
 35               "Try to expand a snippet before point, then call `%s' as usual"
 36               function-symbol)
 37             (let ((yas/fallback-behavior nil))
 38               (unless (and (interactive-p)
 39                            (yas/expand))
 40                 ad-do-it)))))
 41  
 42  (yas/advise-indent-function 'cdlatex-tab)
 43  (yas/advise-indent-function 'org-cycle)
 44  (yas/advise-indent-function 'org-try-cdlatex-tab)
 45  (add-hook 'org-mode-hook 'yas/minor-mode-on)
 46  
 47  
 48  
 49  
 50  
 51  ;cdlatex 
 52  (add-to-list 'load-path
 53  "~/elisp")
 54  (require 'cdlatex)
 55  
 56  
 57  ;fontsize
 58  (set-face-attribute 'default nil :height 150)
 59  
 60  ;fullscreen
 61  (global-set-key [f11]'my-fullscreen)
 62  (defun my-fullscreen ()
 63    (interactive)
 64    (x-send-client-message
 65     nil 0 nil "_NET_WM_STATE" 32
 66     '(2 "_NET_WM_STATE_FULLSCREEN" 0))
 67  )
 68  (defun my-maximized ()
 69    (interactive)
 70    (x-send-client-message
 71     nil 0 nil "_NET_WM_STATE" 32
 72     '(1 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
 73    (interactive)
 74    (x-send-client-message
 75     nil 0 nil "_NET_WM_STATE" 32
 76     '(1 "_NET_WM_STATE_MAXIMIZED_VERT" 0)))
 77  (my-maximized)
 78  
 79  
 80  ;;關閉起動時的那個“開機畫面”
 81  (setq inhibit-startup-message t)
 82  
 83  
 84  ;不要滾動欄
 85  (scroll-bar-mode nil)
 86  
 87  
 88  ;改變emacs標題欄的標題
 89  (setq frame-title-format "%b_Luqing Ye")
 90  
 91  ;讓M-w 能復制內容到剪切板
 92  (setq x-select-enable-clipboard t)
 93  
 94  ;當指針到一個括號時,自動顯示所匹配的另一個括號
 95  (show-paren-mode 1)
 96  
 97  ;ibus
 98  (add-to-list 'load-path "~/.emacs.d/ibus/")
 99  (require 'ibus)
100  (add-hook 'after-init-hook 'ibus-mode-on)
101  (global-set-key (kbd "C-=") 'ibus-toggle) ;;這里既是綁定上面設置的C+=快捷鍵到ibus中
102  (ibus-define-common-key ?\C-\s nil)
103  ;; Use C-/ for Undo command
104  (ibus-define-common-key ?\C-/ nil)
105  ;; Change cursor color depending on IBus status
106  (setq ibus-cursor-color '("red" "black" "green"))
107  
108  
109  
110  ;auctex setting,from  http://www.xemacs.org/Documentation/packages/html/auctex_4.html    
111  (require 'tex-site)
112  (setq TeX-auto-save t)
113  (setq TeX-parse-self t)
114  
115  ;為了使用 AucTeX 方便,為 LaTeX 模式 hook 自動換行,數學公式,reftex 和顯示行號的功能
116  (mapc (lambda (mode)
117        (add-hook 'LaTeX-mode-hook mode))
118        (list 'auto-fill-mode
119              'cdlatex-mode
120              'LaTeX-math-mode
121              'turn-on-reftex
122              'linum-mode))
123  
124  
125  
126  
127  
128  
129  (custom-set-variables
130    ;; custom-set-variables was added by Custom.
131    ;; If you edit it by hand, you could mess it up, so be careful.
132    ;; Your init file should contain only one such instance.
133    ;; If there is more than one, they won't work right.
134   '(show-paren-mode t)
135   '(tool-bar-mode nil))
136  (custom-set-faces
137    ;; custom-set-faces was added by Custom.
138    ;; If you edit it by hand, you could mess it up, so be careful.
139    ;; Your init file should contain only one such instance.
140    ;; If there is more than one, they won't work right.
141   )
142  
143  
144  
145  
146  
147  (setq TeX-save-query nil) ;;autosave latex file before compiling
148  
149  
150  
151  
152  
153  
154  
155  ;;1.This will make sure org gets loaded, and some basic global key combinations are defined
156  ;;2.If you've configured your Emacs initialization file as suggested below, Emacs should enter org-mode whenever you visit or create a file with the extension ".org".
157  (require 'org-install)
158  (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
159  (define-key global-map "\C-cl" 'org-store-link)
160  (define-key global-map "\C-ca" 'org-agenda)
161  (setq org-log-done t)
162  
163  
164  
165  
166  ;;org mode自動換行
167  (add-hook 'org-mode-hook
168      (lambda () (setq truncate-lines nil)))
169  
170  
171  
172  
173  
174  
175  
176  (add-to-list 'load-path "~/elisp")
177  (require 'cdlatex)
178  (require 'texmathp)
179  ;load org-cdlatex-mode for all org files
180  (add-hook 'org-mode-hook 'turn-on-org-cdlatex)
181  
182  
183  
184  
185  
186  ;; all backups goto ~/.backups instead in the current directory
187  (setq backup-directory-alist (quote (("." . "~/.backups"))))
188  
189  
190  ;use pdflatex
191  (setq latex-run-command "pdflatex")
192  
193  
194  ;generate pdf when complile.
195  (setq TeX-PDF-mode t)

在配置文件里,我對自己的emacs進行了主題色彩的設置,設置之前要先安裝主題色彩包,對此我不詳細說了,這里有.在配置文件里,我還設置了Yasnippet,Yasnippet可以讓你使用自定義模板.它的安裝方法在這里.有些好心的外國人已經幫我們做了很多現成的yasnippet模板,在這里.我還安裝了cdLaTeX,cdLaTeX的介紹可以參考這里 .cdLaTeX和yasnippet有點不兼容,他們倆老是爭出頭,有時候一個在,就讓另一個失效了,解決方案在這里.在Linux下,想用ibus輸入法在emacs里起作用的話,就參考這里 .

諸如此類的,細節問題比較多.有一句話叫自己動手豐衣足食,我說的也夠多了,再說就要寫書了.而且,我也是一只菜鳥啊.更多的問題,大家自己google去吧.不過,在此,我還要給各位一個最重要的鏈接.

 

emacs 中 preview-LaTeX 的設置_~ 只談概念 ~_百度空間

 


更新1:Emacs中交換CTRL 和CAPS LOCK鍵后往往會更方便,具體見這里.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM