Show math formula in emacs when coding

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?

Yes. See pretty-symbols package. It can look like this, for example, when coding Python, or any other language: 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. 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)

'c++-mode
'(("\$$<\\(?:code\\|tt\$$>\"?\\)\$$[^<]*?\$$\$$\"?</\\(?:code\\|tt\$$>\\)"
(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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.72306055,"math_prob":0.8512522,"size":1137,"snap":"2019-43-2019-47","text_gpt3_token_len":344,"char_repetition_ratio":0.10767873,"word_repetition_ratio":0.0,"special_character_ratio":0.3403694,"punctuation_ratio":0.12206573,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9743392,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,4,null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-21T20:46:34Z\",\"WARC-Record-ID\":\"<urn:uuid:b5bf6ad8-6abe-4f2f-9626-9e464237e3e8>\",\"Content-Length\":\"147215\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4fcd913c-a551-45eb-8a7c-cf703789d425>\",\"WARC-Concurrent-To\":\"<urn:uuid:6313f140-51eb-4808-b6fb-4afb57d76b8c>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://emacs.stackexchange.com/questions/12089/show-math-formula-in-emacs-when-coding/12098\",\"WARC-Payload-Digest\":\"sha1:OJ43KGQ2ZCJQ644TZK6LTQK24NLV2G5P\",\"WARC-Block-Digest\":\"sha1:SQD56JMH5EAZG6IZZQMUHSCO6ZKPR5OZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570987787444.85_warc_CC-MAIN-20191021194506-20191021222006-00227.warc.gz\"}"}