io12 / org-fragtog

Automatically toggle Org mode LaTeX fragment previews as the cursor enters and exits them
MIT License
397 stars 14 forks source link

Add math-preview as a backend #34

Closed deb75 closed 2 years ago

deb75 commented 2 years ago

Hello,

Is it possible to add math-preview as a backend for svg's generation ?

This feature request is related to an opened request here

Regards

io12 commented 2 years ago

I haven't tried math-preview, but does the code in this comment work?

(defalias #'org-latex-preview #'math-preview-at-point)
(defalias #'org-clear-latex-preview #'math-preview-clear-region)

If this is enough, maybe I can add it to the README or math-preview can add it to its README.

deb75 commented 2 years ago

Yes, it works :-) and I can tell it is much more faster than the dvisvgm backend

Probably, the best way should be to add an entry to org-preview-latex-process-alist, e.g. 'mathjax and set it with (setq org-preview-latex-default-process 'mathjax). But it does not seem compatible at first view or I am not skilled enough.

deb75 commented 2 years ago

With the following settings :

(defalias #'org-latex-preview #'math-preview-at-point)
(defalias #'org-clear-latex-preview #'math-preview-clear-region)

math-preview replaces the current backend of org-fragtog. It works well except when equation is indented :

\begin{equation}
x^2
\end{equation}

works, but

    \begin{equation}
    x^2
    \end{equation}

does not work.

Of course, it still works if I invoke math-preview-at-point by hand.

I asked this question to math-preview maintainer. According to him, it does not seem to be math-preview related.

Do you experience the same issue ?

Regards

io12 commented 2 years ago

It looks like this happens because org-mode considers whitespace before a LaTeX fragment as part of the fragment, but math-preview-at-point doesn't. There is already code to discard whitespace after the fragment, so probably the solution is for org-fragtog to try toggling at the end of the fragment instead of the beginning.

deb75 commented 2 years ago

Hello,

I still experience this issue, is there a simple way to do what you propose. I guess one of org-fragtog's forks did something close with this commit.

But I do not know yet if it works

Regards

EDIT : I do not think it works, but it is true that if I remove all the space before the first line of latex environment, usually it is \begin{..., then org-fragtog works well with math-preview.

When there is some space, org-fragtog correctly finds the latex fragments and disable it when the cursor hovers the latex fragment, but org-fragtog will never enable it again.

io12 commented 2 years ago

Fixed with https://github.com/io12/org-fragtog/commit/e69fb119a7ef3dc39381edd66a20dce9d384fffd. Sorry I didn't get to it until now.