如何在任意模式下内联预览TeX公式?


14

如何在AUCTeX或org-mode之外的其他模式下获取TeX预览?我很想看看jabber,erc和markdown中的TeX方程。如果我曾经转而使用Emacs收发电子邮件,可能我也想在那里查看预览。


1
我没有尝试过,但是您可能想看看mmm-mode哪种缓冲允许多个主要模式
Vamsi 2014年

Answers:


10

我试图重用AUCTeX 预览版乳胶,但失败了,放弃了。

组织模式也提供相同的功能。原来weechat设法重新使用它来自动显示乳胶预览。

我已经从weechat中提取了预览部分,放在一个单独的文件中,您可以从该仓库中获得文件。它也可以作为pxMELPA上的软件包使用(我刚刚提交了它)。

可以交互调用四个入口点:

  • px-preview:渲染当前缓冲区中的所有乳胶片段。
  • px-preview-region:渲染区域中的片段。
  • px-remove:从当前缓冲区中删除所有预览(恢复文本)。
  • px-toggle:切换当前缓冲区中预览的显示。

只要组织模式预览器可以工作,这个就应该。

由于代码仍然很小,我将在下面包含它,但是请安装该软件包或使用回购协议。

;;; px.el --- preview inline latex -*- lexical-binding: t -*-

;; Most of this code comes from weechat-latex.el which in turn uses
;; org-mode previewer.

;; Copyright (C) 2014 Aurélien Aptel <aurelien.aptel@gmail.com>
;; Copyright (C) 2013 Rüdiger Sonderfeld <ruediger@c-plusplus.de>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(require 'org)

(defvar px-temp-file-prefix "px-"
  "Prefix for temporary files.")

(defvar px-temp-directory-prefix "px-"
  "Prefix for temporary directory.")

(defvar px-image-program org-latex-create-formula-image-program
  "Program to convert LaTeX fragments.
See `org-latex-create-formula-image-program'")

(defvar px-temp-dir nil
  "The temporary directory used for preview images.")

(defun px--create-preview (at)
  "Wrapper for `org-format-latex'.
The parameter AT should be nil or in (TYPE . POINT) format.  With TYPE being a
string showing the matched LaTeX statement (e.g., ``$'') and POINT being the
POINT to replace.  If AT is nil replace statements everywhere."
  (org-format-latex px-temp-file-prefix
                    px-temp-dir
                    'overlays
                    "Creating images...%s"
                    at 'forbuffer
                    px-image-program))

(defun px--set-temp-dir ()
  "Set `px-temp-dir' unless it is already set."
  (unless px-temp-dir
    (setq px-temp-dir
          (make-temp-file px-temp-directory-prefix
                          'directory))))

(defun px-preview ()
  "Preview LaTeX fragments."
  (interactive)
  (save-excursion
    (let ((inhibit-read-only t))
      (px--set-temp-dir)
      (org-remove-latex-fragment-image-overlays)
      (px--create-preview nil))))

(defun px-preview-region (beg end)
  "Preview LaTeX fragments in region."
  (interactive "r")
  (let* ((math-regex (assoc "$" org-latex-regexps))
         (regex (nth 1 math-regex))
         (n (nth 2 math-regex))
         matches)
    (save-excursion
      (goto-char beg)
      (while (re-search-forward regex end t)
        (setq matches (cons (cons "$" (match-beginning n)) matches)))
      (let ((inhibit-read-only t))
        (px--set-temp-dir)
        (dolist (i matches)
          (px--create-preview i))))))


(defun px-remove ()
  "Remove LaTeX preview images."
  (interactive)
  (let ((inhibit-read-only t))
    (org-remove-latex-fragment-image-overlays)))

(defun px-is-active? ()
  "Are LaTeX Previews currently displayed?"
  org-latex-fragment-image-overlays)

(defun px-toggle ()
  "Toggle display of LaTeX preview."
  (interactive)
  (if (px-is-active?)
      (px-remove)
    (px-preview)))


(provide 'px)

0

texfrag.el提供了开箱即用的Doxygen注释功能prog-mode

您可以安装texfrag.elmelpa-stable通过M-x package-install RET texfrag RET

运行M-x texfrag-mode RET以激活预览。您将获得带有AUCTeX预览包的所有项目的“预览”菜单。

注意,这texfrag也适用于编辑HTML源代码,eww和org-mode。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.