【问题标题】:Emacs preview-latex to format python comments and docstringsEmacs preview-latex 格式化 python 注释和文档字符串
【发布时间】:2012-11-14 02:57:46
【问题描述】:

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

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

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

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

【问题讨论】:

  • 不知道狮身人面像,安德烈亚斯。谢谢。但是我在编辑它时正在我的python源中寻找渲染的方程。 sphinx 是我想要的,preview-latex 是 pdflatex。
  • 我需要类似的东西用于 doxygen cmets。由于我比 org-mode 中的 cdlatex 更喜欢 preview-mode,所以我做了 preview-package 的手术。结果在那里:github.com/TobiasZawada/texfrag/blob/master/texfrag.el。该软件包的编写使其可以轻松适应其他主要模式。

标签: python emacs latex


【解决方案1】:

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

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 模式并且文件是临时文件,此功能也可以工作.py。

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

【讨论】:

  • 这很好用,但预览是白底黑字,不太适合我使用的主题(如您的图片中所示)。有关如何解决此问题的任何建议?
  • @weemattisnot 如果您将org-latex-create-formula-image-program 设置为'dvipng,预览将始终为黑白。如果它设置为'imagemagick,就像我的情况一样,我相信预览颜色会随着主题而变化。我正在使用zenburn 主题,效果很好。
  • 我的.emacs 文件中有该行。我不知道出了什么问题,但我确实找到了 px-preview 包,它提供了我需要的(最小)功能,一个命令 px-toggle 可以在预览乳胶作为图像之间切换。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2012-12-25
    • 2021-07-06
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多