Is it possible that emacs shows real formulas in the editor if they are written in Latex or similiar? I am asking because it would be much more readable if you are coding (in lets say python or C++) and the comments contain real formulas instead of something like this

// Calculate average for $\mu_1 = \frac{1}{n} ~ \sum_{1=i}^n ~ x_i$

Is there a plugin which compiles Latex code in a comment to a picture and display it in the editor?

4 Answers 4


Yes. See pretty-symbols package. It can look like this, for example, when coding Python, or any other language:

enter image description here

  • Should I use pretty-symbols or latex-pretty-symbols?
    – alper
    Commented Jan 2, 2022 at 20:53

TeXfrag texfrag shows doxygen-formulas embraced by \f[display formula\f] or \f$embedded formula\f$ as images.

The regular expressions and also the functions for parsing the formulas can be customized.

TeXfrag works by:

  1. copying the formulas from the source code buffer to a (hidden) LaTeX buffer
  2. running preview-document there and
  3. moving the overlays back to the source code buffer.

The context menus of preview work for the images in the source code buffer. There is also a minor-mode menu which allows you to generate the images for the full buffer or just the images around point.

Example: The upper buffer shows a section of a doxygen-comment in a c-file with overlaid images for the equation fragments the lower buffer shows the same section with the overlays removed.

emacs screenshot with rendered formulas in doxygen comments

EDIT: Note that TeXfrag has been renamed to texfrag to fulfill the requirements of melpa. texfrag has been reviewed and accepted by melpa. If you have melpa in package-archives you can install texfrag via package-install.


It seems to me that an actual image would be more trouble than it's worth in c++-mode or python-mode. It could work in org-mode though, which has a mechanism for storing the images and refreshing them. Even then, it's awkward for scrolling if the image has a large height.

But you can still fontify bits if you want. For instance, here's my setup for font-locking doxygen tags in C++:

(defface font-lock-doxygen-face
    '((nil (:foreground "SaddleBrown" :background "#f7f7f7")))
  "Special face to highlight doxygen tags such as <tt>...</tt>
and <code>...</code>."
  :group 'font-lock-highlighting-faces)

    (0 (prog1 ()
         (let* ((expr (match-string-no-properties 2))
                (expr-len (length expr)))
           (if (eq 1 expr-len)
               (compose-region (match-beginning 0)
                               (match-end 0)
                               (aref expr 0))
             (compose-region (match-beginning 1)
                             (1+ (match-end 1))
                             (aref expr 0))
             (compose-region (1- (match-beginning 3))
                             (match-end 3)
                             (aref expr (1- expr-len)))))))
    (0 'font-lock-doxygen-face t))))

It will replace <tt>foo</tt> and <code>bar</code> with colored symbols.


Another option to consider is the latex-math-preview package. It allows one to view "real formulas in the editor" in a separate buffer (or to save the formula image to a file). Simply invoke latex-math-preview-expression while the point is on an expression.

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.