Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working #18

Open
kirk86 opened this issue Jul 22, 2019 · 6 comments
Open

Not working #18

kirk86 opened this issue Jul 22, 2019 · 6 comments

Comments

@kirk86
Copy link

kirk86 commented Jul 22, 2019

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

Am I doing somehting wrong here?

@TobiasZawada
Copy link
Owner

TobiasZawada commented Jul 23, 2019

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]
'''

@TobiasZawada
Copy link
Owner

TobiasZawada commented Jul 23, 2019

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-x package-initialize
  • texfrag-version: texfrag-20190606.2049

Result:
image

@kirk86
Copy link
Author

kirk86 commented Jul 23, 2019

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!

@TobiasZawada
Copy link
Owner

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:

  1. Do you actually use python.el shipping with the Emacs distribution? (Maybe, you are using some other package for programming in Python.)
  2. 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.
  3. Which emacs-version and which texfrag-version do you use? Do you install 1.0.1 from melpa-stable or master from melpa?

@kirk86
Copy link
Author

kirk86 commented Jul 26, 2019

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

  1. No I don't use python.el I use elpy
  2. Yes I use your test example
  3. emacs 26.2 and texfrag 20190606.2049

Hope that helps!

@TobiasZawada
Copy link
Owner

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:

  1. emacs -Q
  2. M-x package-initialize
  3. Paste the Elisp source code of Not working  #18 (comment) into the *scratch* buffer
  4. M-x eval-buffer in the *scratch* buffer
  5. Load the test python file
  6. M-x elpy-mode
  7. M-x texfrag-mode
  8. Run TeX -> for document from the texfrag menu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants