Emacs预览latex以格式化python注释和文档字符串

Emacs预览latex以格式化python注释和文档字符串,python,emacs,latex,Python,Emacs,Latex,我最喜欢的Emacs特性之一是预览latex包:它将latex文档中的latex方程作为内联图形呈现。像这样: 我希望在Emacs中为Python注释和函数docstring提供类似的功能。我的文档字符串包含很多数学知识: def proj_levenberg(x,y,wsqrt,A): """ Finds a homography between two sets of 2d points. Specifically, approximately minimize

我最喜欢的Emacs特性之一是预览latex包:它将latex文档中的latex方程作为内联图形呈现。像这样:

我希望在Emacs中为Python注释和函数docstring提供类似的功能。我的文档字符串包含很多数学知识:

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编写这些代码,这样当我浏览代码时,我可以看到呈现的文档字符串和注释


有什么方法可以做到这一点,还是我必须在预览latex上进行surgergy?

IIUC,预览latex中有一个函数,您可以调用该函数来表示“将开始和结束之间的文本呈现为latex公式”。如果你问David Kastrup,他会告诉你这个函数是什么。因此,剩下要做的就是检测您希望以这种方式呈现的代码的哪些部分。

我对您的代码示例进行了一些修改,以便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.
    """
;; 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")
注意,我已经将乳胶碎片包装在
\(
\)

您需要安装组织模式。默认情况下,Org安装在emacs中,但我使用emacs软件包管理器安装了最新的Org模式(版本8.0+)

我的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.
    """
;; 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")

使用此设置,当我执行
M-x组织预览乳胶片段时,我得到以下结果。您可以使用
M-x org-ctrl-c-ctrl-c
删除预览。请注意,我不需要将主模式设置为
org模式
即使缓冲区处于Python模式且文件为temp.py,此函数也可以工作

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


安德烈亚斯,我不知道斯芬克斯。谢谢但在编辑python源代码时,我会在其中查找渲染方程式。斯芬克斯是我想要的,就像预览乳胶是pdflatex一样。我需要类似的东西来评论doxygen。因为我更喜欢预览模式而不是组织模式,所以我对预览包做了手术。结果是:。该软件包的编写使得它可以轻松地适应其他主要模式。这很好,但预览是黑白相间的,不适合我使用的主题(如您的图片)。有没有关于如何解决此问题的建议?@weemattis如果您将
org latex create formula image program
设置为
'dvipng
,预览将始终是黑白相间的。如果像我的例子一样设置为
'imagemagick
,我相信预览颜色会随着主题的变化而变化。我使用的是
zenburn
主题,效果很好。我的
.emacs
文件中有这一行。我不知道怎么回事,但我确实找到了px预览包,它提供了我所需要的(最低限度的)功能,一个命令
px-toggle
,可以在预览乳胶图像和不预览乳胶图像之间切换。