jbyuki / nabla.nvim

take your scientific notes :pencil2: in Neovim
MIT License
581 stars 17 forks source link

request: Support multi-line formulas #56

Closed ayhon closed 1 year ago

ayhon commented 1 year ago

I really like this plugin, and I wish I could use it more often. The problem is that although I use LaTeX a lot for typsetting mathematical notation, this is hardly ever done in .tex files. Most of the time it's in .md files, and more recently I've become interested in doing so for .norg files.

At first I thought this would be a feature I could implement in not too much time, but the literate programming style of the project has left me with no idea how to tackle the change I want to make. I did some research online on how this paradigm works, but I haven't found much that could help me, and the code structure doesn't feel intuitive to change to me. This may be a mix of ntangle.nvim not seeming to work for me (It doesn't detect the .t filetype, and seems to not work. There's also not a lot of documentation on how to set it up, so it may be all on me) and me feeling overwhelmed.

I just wanted to open up this issue to request for the feature I wanted: having the toggle_virt work in other filetypes. If implementing it is too much of a chore, I also wouldn't mind being pointed to someplace to better learn how to contribute.

If these types of issues are not permitted (It does not fit with the templates in the project), feel free to delete it.

Regardless, have a nice day.

max397574 commented 1 year ago

I won't be able to do it myself but it should be possible to do this by changing the treesitter node types used (@jbyuki )

jbyuki commented 1 year ago

I don't have too much time now to look into myself either, but if I understood the request correctly, this should work already. I'm already using .md files to write my occasional notes using nabla.nvim. Here is an example for a concrete example : gist. Open it, then by just executing toggle_virt, the formulas get rendered in nvim.

For why it's not working in your case, it might be a missing treesitter parser (markdown, latex, ... not sure which one but might be worth to install them all). For neorg, I'm not too familiar but it should also work straightaway or as @max397574 says, a few treesitter nodes need to be adapted.

jbyuki commented 1 year ago

If you are averted to make changes in the codebase (which I understand), it's always possible to PR changes in the .lua files only and I can try to adapt it to the literate source files. Although I'm aware the generated .lua files are kind of hard to read and modify.

ayhon commented 1 year ago

Thank you both for the quick responses

I don't have too much time now to look into myself either, but if I understood the request correctly, this should work already. I'm already using .md files to write my occasional notes using nabla.nvim. Here is an example for a concrete example : gist. Open it, then by just executing toggle_virt, the formulas get rendered in nvim.

Hmm, that differs a bit from my experience. I'll look further into it, but I'm pretty sure I have all my parsers up to date.

For why it's not working in your case, it might be a missing treesitter parser (markdown, latex, ... not sure which one but might be worth to install them all). For neorg, I'm not too familiar but it should also work straightaway or as @max397574 says, a few treesitter nodes need to be adapted.

By "treesitter nodes need to be adapted" in which way should they be adapted? Should they include a specific type to delimit a math area?

max397574 commented 1 year ago

with norg it should be possible to insert tex code blocks with stuff inside if you then have the latex parser installed nabla should work just fine (I've done that in the past myself)

jbyuki commented 1 year ago

It all boils down to nabla.nvim detecting "math zones". Currently the function is private in the plugin but it should be easily extractable. It's located here: https://github.com/jbyuki/nabla.nvim/blob/eb543dc41001413ceccd1c66f0d97a2cfb6e68a2/lua/nabla/utils.lua#L19-L36 try to run it manually by placing the cursor on equations and see what comes out. That could be a first way to debug the issue.

ayhon commented 1 year ago

Sorry for the delay in my response. I finally found some time to dedicate to this issue.

I think this whole problem may have been born from a misconception I had. The reason I thought detection didn't work was because I was testing it with a display math environment, that is, math in between $$. If it's just math between $ then it works fine. But math between $$ doesn't work neither in md files nor in tex files.

Video

I don't know if this is desired behavior. I tested with other ways of making a display math environment, like `. It also seems like I found a "bug" or some kind of problem with the parser.

Video I failed to show in the video that this same equation is shown fine when in between $, both with popup and toggle_virt.

In case it's useful, here's the file I used to test this

jbyuki commented 1 year ago

Indeed we're relying on the latex parser to do give us math nodes. But in this case, this is nabla's issue and not the parser. I haven't checked it but in the file you show, I'm pretty certain the latex parser detects it as a math node. It's valid Latex after all. The problem is that multi-line math expressions is not supported yet (implementation pending). It requires some gymnastics with the virt_lines to support it so for now I'm just writing every formula on a single line. Ideally, multi-line should be supported. It's the last bullet point on the virt_lines thread 😄 .

max397574 commented 1 year ago

I'm sure I used multiline formulas before though

jbyuki commented 1 year ago

The implementation is a bit risky (i.e. it assumes the underlying latex formula is longer than the overlaying ASCII art) but it should work now.

brglng commented 2 months ago

It still does not render correctly.

图片
# abc

$$
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
$$