Open kirk86 opened 5 years ago
The formula is probably part of a doc-string.
Python-mode is currently supported through prog-mode
and prog-mode
only supports TeX-fragments in comments.
For Python comments run from an unescaped out-of-string hash-character # and end with the next newline.
You can change that through the following lines in your init file. TeX-fragments are recognized anywhere in the code with that change.
(defvar texfrag-comments-only) ;; Defined in texfrag.el
(defun texfrag-python ()
"texfrag setup for Python."
(setq texfrag-comments-only nil))
(eval-after-load "texfrag"
(lambda ()
(add-to-list 'texfrag-setup-alist '(texfrag-python python-mode))))
Note that texfrag works with LaTeX. So your comment should look like following:
'''
Some string-comment:
n_iterations: float
The number of training iterations the algorithm will tune the weights for.
\f[\min_A \sum_{X,y,w} w \left\| \frac{A_{12} x}{A_3 x_1} - y\right\|^2\f]
'''
You can also use the following Elisp code for Python. If you install it in your init file documentation strings and comments are searched for texfragments.
(declare-function 'python-info-docstring-p "python")
(defun texfrag-python-next-frag (&optional bound)
"Search for TeX fragments in comments and `python-info-docstring-p'.
Set `texfrag-comments-only' to nil to use this function as
`texfrag-next-frag-function'."
(let (found)
(while (and
(setq found (texfrag-next-frag-default bound))
(null
(or (nth 4 (syntax-ppss)) ;; in comment
(python-info-docstring-p)))))
found))
(defun texfrag-python-previous-frag (&optional bound)
"Search for TeX fragments in comments and `python-info-docstring-p'.
Set `texfrag-comments-only' to nil to use this function as
`texfrag-previous-frag-function'."
(let (found)
(while (and
(setq found (texfrag-previous-frag-default bound))
(null
(or (nth 4 (syntax-ppss)) ;; in comment
(python-info-docstring-p)))))
found))
(defun texfrag-python ()
"Texfrag setup for Python."
(setq texfrag-comments-only nil
texfrag-next-frag-function #'texfrag-python-next-frag
texfrag-previous-frag-function #'texfrag-python-previous-frag))
(eval-after-load "texfrag"
(lambda ()
(add-to-list 'texfrag-setup-alist '(texfrag-python python-mode))))
If you test this code for a while and give me some positive feedback I will add it to texfrag
.
My first rudimentary test:
# Some fragment in a comment interpreted by texfrag \f$\sqrt{x^2 + y^2}\f$.
def fun(A,x,y,w):
'''
Some documentation string:
n_iterations: float
The number of training iterations the algorithm will tune the weights for.
\f[\min_A \sum_{X,y,w} w \left\| \frac{A_{12} x}{A_3 x_1} - y\right\|^2\f]
'''
print "Some fragment \f[\sqrt{x^2 + y^2}\f] in a string not interpreted as TeX."
return {sum(A[i][j]*x[j] for j in length(A[0])) for i in length(A)};
Test with
emacs-version
: GNU Emacs 26.2 (build 2, i686-pc-linux-gnu, GTK+ Version 3.22.30) of 2019-04-12
emacs -Q
and manual invocation of M-x package-initialize
texfrag-version
: texfrag-20190606.2049
Result:
Hi thanks for the suggestions. I"m still not able to properly render your test example. Initially I was using emacs on the terminal which made me realize that it's not gonna work there so I switched to gui and now it properly produces the .tex file filled with everything but when I run preview at point still doesn't render the equation.
I see that inside the texfrag folder there's .pdf file generated containing the equation from your initial oneliner comment
# Some fragment in a comment interpreted by texfrag \f$\sqrt{x^2 + y^2}\f$.
But I would like this
\f[\min_A \sum_{X,y,w} w \left\| \frac{A_{12} x}{A_3 x_1} - y\right\|^2\f]
to be generated.
Is there any possible way to make this work for emacs terminal?
Thanks!
texfrag
bases on AucTeX's preview
package. If the preview
package works in LaTeX documents texfrag
should work too.
One basic pre-condition for a working preview
package is a working LaTeX installation. Do you have one?
Maybe, I should mention that explicitly in README.md.
About the missing display formula:
python.el
shipping with the Emacs distribution? (Maybe, you are using some other package for programming in Python.)emacs-version
and which texfrag
-version do you use? Do you install 1.0.1 from melpa-stable
or master
from melpa
?If the
preview
package works in LaTeX documents
Yes it works fine
pre-condition for a working
preview
package is a working LaTeX installation
Yes I have that as well
python.el
I use elpy
Hope that helps!
elpy-mode
is a minor mode activated together with python-mode
.
I've tested texfrag
20190606.2049 with activated elpy-mode
.
It works flawlessly.
Could you also test with:
emacs -Q
M-x package-initialize
*scratch*
bufferM-x eval-buffer
in the *scratch*
bufferM-x elpy-mode
M-x texfrag-mode
TeX -> for document
from the texfrag menu
Hi just installed and tried your package on python code comments and nothing happens when executing preview at point. It simply generates a .tex file with variables pointing to the .py file.![image](https://user-images.githubusercontent.com/2902390/61655382-603ac380-acb6-11e9-96bd-1031000838b9.png)
Am I doing somehting wrong here?