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

有没有办法做到这一点,或者我必须在预览 latex 上做手术吗?

最佳答案

我稍微修改了您的代码示例,以便 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 之间包装了 latex 片段

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

我的 emacs 设置中有以下内容用于预览 latex 片段:
;; 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 不会保存每个文件的预览状态。

关于python - Emacs preview-latex 格式化 python 注释和文档字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13372362/

10-13 05:06