[英]Emacs preview-latex to format python comments and docstrings
我最喜歡的Emacs功能之一是Preview-latex軟件包:它將LaTeX文檔中的LaTeX方程呈現為嵌入式圖形。 像這樣:
我想在Emacs中為Python注釋和函數docstrings提供類似的功能。 我的文檔字符串中包含很多數學運算:
def proj_levenberg(x,y,wsqrt,A):
"""
Finds a homography between two sets of 2d points.
Specifically, approximately minimize the weighted image plane
error
min_A sum_{X,y,w} w ||(A_12 x_) / (A_3 x_) - y||^2
where x_=[x;1], A_12 are the first two rows of A and A_3 is the
third row of A.
...
我想用LaTex編寫這些代碼,以便在瀏覽代碼時可以看到呈現的文檔字符串和注釋。
有沒有辦法做到這一點,或者我必須在Preview-Latex上進行Surgergy?
我對您的代碼示例進行了一些修改,以便emacs了解需要用乳膠片段替換的哪一部分:
def proj_levenberg(x,y,wsqrt,A):
"""
Finds a homography between two sets of 2d points.
Specifically, approximately minimize the weighted image plane
error
\(min_A sum_{X,y,w} w ||(A_12 x_) / (A_3 x_1) - y||^2\)
where \(x_1\)=[x;1], \(A_{12}\) are the first two rows of A and \(A_3\) is the
third row of A.
"""
請注意,我已經將乳膠碎片包裹在\\(
和\\)
源之間
您需要安裝組織模式。 Org默認情況下安裝在emacs中,但是我使用emacs軟件包管理器安裝了最新的組織模式(版本8.0+)。
我在emacs設置中具有以下內容,用於預覽乳膠碎片:
;; Previewing latex fragments in org mode
(setq org-latex-create-formula-image-program 'imagemagick) ;; Recommended to use imagemagick
(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
(setq LaTeX-command "latex -shell-escape")
通過此設置,當我執行Mx org-preview-latex-fragment
,得到以下內容。 您可以使用Mx org-ctrl-c-ctrl-c
刪除預覽。 請注意,我不需要將org-mode
模式設為org-mode
。 即使緩沖區處於Python模式並且文件為temp.py,此功能也可以工作。
值得注意的是,每次您重新打開該文件時,都必須運行此功能。 emacs不會保存每個文件的預覽狀態。
IIUC,預覽膠乳中有一個函數,您可以調用它說“將START和END之間的文本作為LaTeX公式呈現”。 如果您問David Kastrup,他會告訴您該功能是什么。 因此,剩下要做的就是檢測要以這種方式呈現代碼的哪些部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.