4

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

预览-乳胶屏幕截图

我想在 Emacs 中为 Python 注释和函数文档字符串提供类似的功能。我的文档字符串中有很多数学:

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

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

4

2 回答 2

5

我稍微修改了您的代码示例,以便 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.
    """

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

您需要安装 org 模式。org 默认安装在 emacs 中,但我使用 emacs 包管理器安装了最新的 org 模式(版本 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")

我的 emacs 组织模式设置

我的 emacs 乳胶设置

当我这样做时,使用此设置M-x org-preview-latex-fragment,我得到以下信息。您可以使用注意删除预览M-x org-ctrl-c-ctrl-c ,我不需要将主要模式设置为org-mode. 即使缓冲区处于 Python 模式并且文件是 temp.py,此函数也可以工作。

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

乳胶片段预览

于 2014-06-11T14:49:44.660 回答
1

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

于 2012-11-14T13:55:14.837 回答