Skip to content

Feature request: Adding support for math (inline and blocks) #6

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

Closed
cnrrobertson opened this issue Mar 22, 2024 · 17 comments
Closed

Feature request: Adding support for math (inline and blocks) #6

cnrrobertson opened this issue Mar 22, 2024 · 17 comments
Labels
enhancement New feature or request

Comments

@cnrrobertson
Copy link

One reason I use markdown previewers is to preview math surrounded by $ or $$ or ```math in LaTeX formatting (see here if you need some examples from Github).

It would be awesome if this plugin could overlay the LaTeX text as unicode in the math areas. Of course, implementing a custom parser to do so would probably be overkill, but there is a great Python utility here that could be used (as an optional dependency for your package perhaps). In fact, there is Vim plugin that uses this utility to replace LaTeX with rendered text here. From their example it could be really straightforward to implement.

If this is of interest, I could possibly put together a draft PR with the implementation.

One catch is that I don't have any experience working with treesitter, so I'm not sure how easy it is to get the "latex" captures. I think they are there, I'd just need a few pointers.

@MeanderingProgrammer
Copy link
Owner

Hi, and thanks for the suggestion. I can definitely understand the use for something like this.

I don't personally use math expressions often so wouldn't be up for maintaining an entire parser, and it does seem like overkill as you mention.
I'll try using the pylatexenc library and see how it turns out, if it works nicely will add the feature. Otherwise will follow up here to see if I can add some support for this kind of thing.

There is definitely a set of features that I don't plan on supporting (like rendering mermaid diagrams) that just get very complex but if there's a way to offload the hard part to a library may as well include it with some instructions.

@MeanderingProgrammer MeanderingProgrammer added the enhancement New feature or request label Mar 22, 2024
@MeanderingProgrammer
Copy link
Owner

I have added a basic level of support for LaTeX using the pylatexenc library as you suggested in: 138a796

I'm honestly not sure if I like it, while you do get the rendered characters, you also lose highlighting within the expression. I guess up to the individual on which they prefer.

Let me know what you think and if you have any other suggestions, thanks!

@cnrrobertson
Copy link
Author

Wow great! Thanks for getting this in so quickly. I agree that its inconvenient to lose the highlighting, but its a great way to get a preview of the math without having to render the whole document.

One thing I did want to ask about are multiline expressions. Currently, if I put in for example:

$$ 
f(x,y) = \sqrt(y) + \frac{x^2}{4y}
$$

the virtual text only replaces the first line with $$. Looking at the source it seems this might be a limitation of the treesitter parser?

@MeanderingProgrammer
Copy link
Owner

Potentially, it's either:

  • A limitation of the parser
  • There not being a standard way to identify latex within markdown
  • I didn't handle it correctly (probably this one)

Github flavored markdown has some of it's own features like the "```math" identifier, which I don't think is supported anywhere else so I think there's going to be some gaps no matter what.

I'll take a look into multi-line support.

MeanderingProgrammer added a commit that referenced this issue Mar 25, 2024
## Details

As a follow up from:
#6,
when LaTeX formulas are multi-line we end up showing the formula
on the first line and not overwriting the actual contents, which
does not look great.

Rather than attempting to overlay the formula as before, use
virtual lines above the text itself.

This is different from the standard rendering experience of an overlay
but I think is generally a nicer experience.

This will have some issues if the inline LaTeX is surrounded by other
text, which seems like an okay trade off.
@MeanderingProgrammer
Copy link
Owner

I've updated the LaTeX logic to display formulas above the enclosing blocks rather than trying to inline them: da85a5e.

Let me know how this works for you! I don't have any LaTeX markdown files handy so can't quite tell what makes sense to do.

The real issue would come from many small inline formulas, I imagine those would become less readable.

@cnrrobertson
Copy link
Author

Wow, thanks again for the very quick response. I like the idea of putting them above - it's not as pretty, but it gives a great preview and keeps the syntax highlighting in the latex inlines and blocks.

You're right about having a lot of small inline formulas, but I don't think that is very common. I have a few readmes that are math heavy and the previews look great for those.

Thanks again!

@MeanderingProgrammer
Copy link
Owner

Awesome, glad its working well enough!

Feel free to suggest ideas / changes after interacting with it some more!

@life00
Copy link

life00 commented Jul 26, 2024

Hi @MeanderingProgrammer. I wanted to comment on this feature a little bit and suggest some improvements.

The first thing is that the performance is pretty horrible, or at least the start up time when the file is initially loaded. 200 lines file with a bunch of equations takes to initialize for about 4 seconds when first opened. Well, it works but other tools that I've used did not have this problem.

Also I would like to suggest to replace the code itself with what is rendered, rather than displaying the render above the code. I think this makes more sense.

I would suggest maybe if its feasible try to implement the concealment yourself in lua (maybe through treesitter?). You might find the following projects relevant. A long time ago I used https://github.com/preservim/vim-markdown which supports concealment of LaTeX math pretty well, however I switched because of performance issues and it overwriting syntax. Currently I am using https://github.com/vim-pandoc/vim-pandoc-syntax which is simpler and runs much smoother, however its not ideal.

I don't know either vimscript or lua much so I am afraid I am not able to help. This is the only crappy part in my neovim setup and I am not sure how to fix it, so unless I find a good alternative I was planning to maybe learn lua and write my own plugin for this.

@life00
Copy link

life00 commented Jul 26, 2024

@life00
Copy link

life00 commented Jul 27, 2024

Aaand I found my dream plugin I was talking about: https://github.com/ryleelyman/latex.nvim

After researching for a while I understood how to implement math concealment in (neo)vim. The old way is to use syntax match ... as linked in previous comment. The new recommended way is through treesitter which requires creating creating custom scheme queries. The above mentioned plugin does that flawlessly.

@MeanderingProgrammer
Copy link
Owner

Thanks for the interest and the enthusiasm!

LaTeX is not something that I use all that often so maintaining a set of rules like in latex.nvim is not something I'm up for. But I get the preference. If you want to keep this plugin around for other markdown related things you can disable the latex feature:

require('render-markdown').setup({
    latex = { enabled = false },
})

And use it in tandem with latex.nvim. I don't see a reason why the 2 would interfere with each other.

@SichangHe
Copy link

Took me a while to figure out, but latex.nvim needs conceallevel = 2 while render-markdown.nvim sets it to 3 by default. Just needed to override that.

@pyrho
Copy link

pyrho commented Oct 9, 2024

For anyone else looking I have a setup using nabla.nvim working with render-markdown nicely.

CleanShot 2024-10-09 at 21 00 01@2x

Here's my setup with lazy.nvim, for nabla:

{
	"jbyuki/nabla.nvim",
	event = "VeryLazy",
	config = function()
		require("nabla").enable_virt({ autogen = true })
	end,
}

for render-markdown:

return {
	"MeanderingProgrammer/render-markdown.nvim",
	event = "VeryLazy",
	dependencies = { "nvim-treesitter/nvim-treesitter", "nvim-tree/nvim-web-devicons" },
	config = function()
		require("render-markdown").setup({
			file_types = { "markdown"},
			latex = { enabled = false },
			win_options = {
				conceallevel = {
					default = vim.api.nvim_get_option_value("conceallevel", {}),
					rendered = 2, -- <- especially this, so that both plugins play nice
				},
			},
		})
	end,
}

@MeanderingProgrammer
Copy link
Owner

Added this as another option in the inline latex doc: https://github.com/MeanderingProgrammer/render-markdown.nvim/blob/main/doc/limitations.md#latex-formula-positioning

Thanks!

@pyrho
Copy link

pyrho commented Oct 9, 2024

Awesome thank you! I realized I made a mistake in the config for nabla require("nabla").enable_virt({ autogen = true }) should be called in a buffer, not at config time. Sorry 😬 .

edit: to be clear no config call required.

@MeanderingProgrammer
Copy link
Owner

Ah, thanks for letting me know, I should have tried it out first in either case.

I've added a new on -> attach callback you can configure to trigger nable.nvim for you: 8b4149b

This is in case you want the plugin to always render latex rather than triggering it through a keymap or otherwise.

Updated the documentation to reflect this: https://github.com/MeanderingProgrammer/render-markdown.nvim/blob/main/doc/limitations.md#latex-formula-positioning.

@pyrho
Copy link

pyrho commented Oct 10, 2024

Simply incredible, thanks !

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

No branches or pull requests

5 participants