TobiasZawada / texfrag

Emacs package for previewing LaTeX fragments such as in doxygen comments.
GNU General Public License v3.0
68 stars 4 forks source link

Not working #18

Open kirk86 opened 5 years ago

kirk86 commented 5 years ago

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 commented 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]
'''
TobiasZawada commented 5 years ago

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

Result: image

kirk86 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 https://github.com/TobiasZawada/texfrag/issues/18#issuecomment-514104763 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