Emacs preview-latex for formatting comments and python docstrings

One of my favorite Emacs features is the latex preview package: it displays LaTeX equations in LaTeX documents as an embedded image file. Like this:

preview-latex screenshot

I would like for similar functions in the Emacs comments for Python and the docstrings functions. My docs have a lot of math in them:

def proj_levenberg(x,y,wsqrt,A): """ Finds a homography between two sets of 2d points. Specifically, approximately minimize the weighted image plane error min_A sum_{X,y,w} w ||(A_12 x_) / (A_3 x_) - y||^2 where x_=[x;1], A_12 are the first two rows of A and A_3 is the third row of A. ... 

I would like to write them in LaTex so that when I look at my code I see processed docstrings and comments.

Is there a way to do this or do I need to do surgery for preview latex?

+4
source share
2 answers

I modified your sample code a bit to make emacs understand how much of this needs to be replaced with latex fragments:

 def proj_levenberg(x,y,wsqrt,A): """ Finds a homography between two sets of 2d points. Specifically, approximately minimize the weighted image plane error \(min_A sum_{X,y,w} w ||(A_12 x_) / (A_3 x_1) - y||^2\) where \(x_1\)=[x;1], \(A_{12}\) are the first two rows of A and \(A_3\) is the third row of A. """ 

Please note that I wrapped the latex fragments between \( and \) Source

You need to set org mode. Org is installed by default in emacs, but I have the latest org mode (version 8.0+) installed using the emacs package manager.

I have the following in my emacs setup for previewing latex fragments:

 ;; Previewing latex fragments in org mode (setq org-latex-create-formula-image-program 'imagemagick) ;; Recommended to use imagemagick (load "auctex.el" nil tt) (load "preview-latex.el" nil tt) (setq LaTeX-command "latex -shell-escape") 

My org mode setting for emacs

My latex installation for emacs

With this setting, when I do Mx org-preview-latex-fragment , I get the following. You can remove the previews with Mx org-ctrl-c-ctrl-c Note that I do not need the main mode to be org-mode . This function worked even where the buffer was in Python mode and the file was temp.py.

The trick is that this function should be run every time you reopen this file; emacs does not save the preview state of each file.

Latex fragment preview

+5
source

IIUC, there is a function in preview latex that you can call to say "visualize text between START and END as a LaTeX formula." If you ask David Kastrup, he will tell you what this function is. So what remains to be done to determine which parts of your code you want to do this way.

+1
source

Source: https://habr.com/ru/post/1445909/


All Articles