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

moo*_*oos 4 python emacs latex

我最喜欢的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.
    ...
Run Code Online (Sandbox Code Playgroud)

我想在LaTex中编写这些内容,这样当我浏览代码时,我可以看到呈现的文档字符串和注释.

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

Kau*_*odi 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.
    """
Run Code Online (Sandbox Code Playgroud)

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

您需要安装org模式.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")
Run Code Online (Sandbox Code Playgroud)

我为emacs设置的组织模式

我的胶乳设置为emacs

在我这样做时M-x org-preview-latex-fragment,我得到了以下内容.您可以使用M-x org-ctrl-c-ctrl-c Noteorg-mode删除预览,我不需要具有主要模式.即使缓冲区处于Python模式且文件为temp.py,此函数也能正常工作.

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

Latex片段预览