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
    Commented Sep 24, 2014 at 21:48

2 Answers 2


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 <[email protected]>
;; Copyright (C) 2013 Rüdiger Sonderfeld <[email protected]>

;; 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
;; 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
                    "Creating images...%s"
                    at 'forbuffer

(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

(defun px-preview ()
  "Preview LaTeX fragments."
    (let ((inhibit-read-only t))
      (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))
      (goto-char beg)
      (while (re-search-forward regex end t)
        (setq matches (cons (cons "$" (match-beginning n)) matches)))
      (let ((inhibit-read-only t))
        (dolist (i matches)
          (px--create-preview i))))))

(defun px-remove ()
  "Remove LaTeX preview images."
  (let ((inhibit-read-only t))

(defun px-is-active? ()
  "Are LaTeX Previews currently displayed?"

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

(provide 'px)
  • I am getting following error: Symbol’s value as variable is void: org-latex-fragment-image-overlays
    – alper
    Commented Aug 3, 2020 at 14:16

texfrag.el renders LaTeX fragments in buffers whose major mode is registered as or derived from a major-mode in texfrag-setup-alist. It uses preview.el from AucTeX for generating the overlays with the LaTeX images.

texfrag.el works out of the box for doxygen comments in major modes derived from 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 and acknowledge you have read our privacy policy.

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