Not working
kirk86 opened this issue · 6 comments
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 called with
emacs -Q
and manual invocation of M-xpackage-initialize
texfrag-version
:texfrag-20190606.2049
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:
- Do you actually use
python.el
shipping with the Emacs distribution? (Maybe, you are using some other package for programming in Python.) - Do you use my test example for testing? If not is the display formula within a comment or a documentation string of a class or a function? The second proposed solution does intentional only work for those cases.
- Which
emacs-version
and whichtexfrag
-version do you use? Do you install 1.0.1 frommelpa-stable
ormaster
frommelpa
?
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
- No I don't use
python.el
I useelpy
- Yes I use your test example
- emacs 26.2 and texfrag 20190606.2049
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
- Paste the Elisp source code of #18 (comment) into the
*scratch*
buffer M-x eval-buffer
in the*scratch*
buffer- Load the test python file
M-x elpy-mode
M-x texfrag-mode
- Run
TeX -> for document
from the texfrag menu