lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.4k stars 388 forks source link

`vimtex-i$` works in markdown math `\(...\)` but not `$...$` #2978

Closed camoz closed 3 weeks ago

camoz commented 1 month ago

Description

Hi,

quick question: is there any obvious reason why vimtex-i$ and vimtex-a$ would work in markdown math blocks using \(...\) and \[...\], but not $...$ and $$...$$ (which is the case for my setup)?

I'm using Neovim with treesitter and load some VimTeX stuff via after/ftplugin/markdown.lua, including running call vimtex#init().

If there is no obvious reason for that then I would post more details about my setup.

Thanks!

Edit: Corrected typo in headline and description; of course I meant vimtex-i$ and not vimtex-im.

lervag commented 1 month ago

Are you using treesitter for highlighting Markdown? What's the output of :Inspect inside $...$ and \(...\), respectively?

camoz commented 1 month ago

Output of :Inspect inside $...$:

Treesitter
  - @spell.markdown links to @spell markdown                                                                                                     
  - @nospell.latex links to @nospell latex                                                                                                       
  - @markup.math.latex links to @markup.math latex                                                                                               
  - @function.latex links to @function latex                                                                                                     
  - @nospell.latex links to @nospell latex                                                                                                       

No idea why latex is showing up here, since I have disabled it? Maybe that does not work for embedded languages?

Output of :Inspect inside \(...\):

Treesitter                                                                                                                             
  - @spell.markdown links to @spell markdown                                                                                                     

Hmm, but here no latex is showing up...

Edit: In a way, this is kind of the correct behavior, since $...$ is used much more commonly than \(...\) in markdown. But for things like apy, having the latter working too would be useful...

camoz commented 1 month ago

I'm assuming (though I'm not sure), that the issue I have is due to the missing detection of \(...\) as embedded latex by the treesitter markdown parser, as the output of :Inspect in my last comment shows. If that makes sense and you cannot think of another reason why the issue is present, feel free to close this issue!

I think one way to solve this for my use case would be to modify the treesitter markdown parser. I guess this change won't be accepted upstream as the $/$$ math delimiters are much more common in markdown and I don't want to maintain my own fork of it. I guess, writing anki cards with apy is the only time I use the (fairly non-standard) \(...\) for latex/mathjax in markdown. Maybe I will try to solve it via apy then.

lervag commented 1 month ago
  • Yes, I'm using treesitter for highlighting Markdown (everything except latex).

This is the problem. The i$ text object and a couple of other things rely on the VimTeX syntax highlighting. This is not treesitter based and it requires that you use the regexp based syntax highlighting for Markdown in a way that also loads VimTeX for the nested highlighting of math constructs.

I actually wrote about this very thing in :help vimtex-syntax, although only in the context of LaTeX files. But it holds for Markdown as well.

See also :help vimtex-faq-treesitter, notice the second question there.

Also, since it is probably not obvious: vimtex#init() does not load syntax highlighting.

Let me know if you need more clarification and/or help!

camoz commented 1 month ago

I have read :help vimtex-syntax, but I'm still confused why vimtex-i$ works in markdown math \(...\) but not $...$...

See also :help vimtex-faq-treesitter, notice the second question there.

I have already read that and also, as mentioned in my OP, if I set additional_vim_regex_highlighting = { "latex", "markdown" } the issue persists...

Not surprisingly, currently, vimtex's syntax highlighting in math mode also doesn't work.

[...] it requires that you use the regexp based syntax highlighting for Markdown in a way that also loads VimTeX for the nested highlighting of math constructs.

I think the highlighted part is the crucial part here... I'm not sure how to do that, even though I searched quite a bit. I think for some people it worked if they had another markdown plugin like https://github.com/preservim/vim-markdown installed.

But, still I'm curious about my original question -- why does vimtex-i$ work inside \(...\) (in markdown files)?

lervag commented 1 month ago

I have read :help vimtex-syntax, but I'm still confused why vimtex-i$ works in markdown math \(...\) but not $...$...

But, still I'm curious about my original question -- why does vimtex-i$ work inside \(...\) (in markdown files)?

Ok; with your current setup, after opening a Markdown file: What's the output of :omap i$?

See also :help vimtex-faq-treesitter, notice the second question there.

I have already read that …

Good!

Not surprisingly, currently, vimtex's syntax highlighting in math mode also doesn't work.

I believe it is because VimTeX syntax highlighting is not really loaded at all.

[...] it requires that you use the regexp based syntax highlighting for Markdown in a way that also loads VimTeX for the nested highlighting of math constructs.

I think the highlighted part is the crucial part here...

Yes, agreed.

I'm not sure how to do that, even though I searched quite a bit. I think for some people it worked if they had another markdown plugin like https://github.com/preservim/vim-markdown installed.

Yes. Ok, so, plugins like "preservim/vim-markdown" already contains the necessary syntax rules. Thus it immediately becomes "easier" in these cases. However, it is not very hard to add the necessary rules and use the built-in markdown syntax plugin. You only need to add a few rules.

I've pushed an example with a minimal configuration that should show approximately how to make that work: https://github.com/lervag/vimtex/tree/master/test/example-markdown

The main point is to add the rules that will load the syntax/tex.vim as the nested syntax group for $ ... $ and similar constructs. And to use an autocommand or similar to execute call vimtex#init() to load the mappings.

The minimal example works as expected for me. I believe, as mentioned before, that the reason it does not work for you is mainly because you do not have the correct syntax rules.

lervag commented 1 month ago

See also the related #2874.