14

How can I get TeX previews inside modes other than AUCTeX or org-mode? I'd love to see TeX equations in jabber, erc and markdown. If I ever switch to Emacs for email, I'd presumably like to see previews there too.

  • 1
    I have not tried this, but you might want to look at mmm-mode which allows multiple major modes in a buffer – Vamsi Sep 24 '14 at 21:48
9

I've tried to re-use AUCTeX preview-latex but failed and gave up.

Org-mode also provide the same feature. Turns out weechat managed to re-use it to automatically show latex preview.

I've extracted the preview part from weechat in a separate file which you can get on this repo. It's also available as the px package on MELPA (I've just submited it).

There are 4 entry points which can be called interactively:

  • px-preview: render all latex fragments in the current buffer.
  • px-preview-region: render fragments in the region.
  • px-remove: remove all previews from the current buffer (restore the text).
  • px-toggle: toggle display of previews in the current buffer.

As long as org-mode previewer works, this one should.

Since the code is still pretty small I'll include it below, but please install the package or use the repo.

;;; 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

There is also texfrag.el which works out of the box for doxygen comments in prog-mode.

You can install texfrag.el from melpa-stable via M-x package-install RET texfrag RET.

Run M-x texfrag-mode RET to activate the preview. You get the Preview menu with all the items of the AUCTeX preview package.

Note, that texfrag also works for editing html source, eww, and org-mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.