Emacs preview-latex用于格式化python注释和文档字符串

时间:2012-11-14 02:57:47

标签: python emacs latex

我最喜欢的Emacs功能之一是preview-latex软件包:它将LaTeX文档中的LaTeX方程式呈现为内联图形。像这样:

preview-latex screenshot

我想在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中编写这些内容,以便在浏览代码时,我可以看到呈现的文档字符串和注释。

有没有办法做到这一点,还是我必须在预览乳胶上进行手术?

2 个答案:

答案 0 :(得分:5)

我稍微修改了你的代码示例,以便emacs了解哪些部分需要用latex片段替换:

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.
    """

请注意,我已将乳胶碎片包裹在\(\) Source

之间

您需要安装org模式。默认情况下,在emacs中安装了Org,但我使用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")

My org mode setup for emacs

My latex setup for emacs

当我M-x org-preview-latex-fragment进行此设置时,我得到以下内容。您可以使用M-x org-ctrl-c-ctrl-c 删除预览。请注意,我不需要将主要模式设为org-mode此功能即使在缓冲区处于Python模式且该文件是temp.py.

问题是,每次重新打开该文件时都必须运行此函数; emacs不保存每个文件的预览状态。

Latex fragment preview

答案 1 :(得分:1)

IIUC,预览乳胶中有一个功能,您可以调用它来说“将START和END之间的文本渲染为LaTeX公式”。如果你问David Kastrup,他会告诉你这个功能是什么。所以剩下要做的就是检测你想用这种方式渲染代码的哪些部分。