Skip to content
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

[Question] Combine default markdown syntax and vimtex syntax? #2874

Closed
Bekaboo opened this issue Jan 26, 2024 · 3 comments
Closed

[Question] Combine default markdown syntax and vimtex syntax? #2874

Bekaboo opened this issue Jan 26, 2024 · 3 comments
Labels

Comments

@Bekaboo
Copy link

Bekaboo commented Jan 26, 2024

Description

Some background:

  1. I am aware that issues like vimtex#syntax#in_mathzone() not work correctly in markdown file. #2346 and other similar issues exist, but all of them seem to require installing vim-markdown. I want to do it without the plugin since I all I need is math concealing
  2. I am using neovim with treesitter disabled for markdown and tex files

I want to combine the default markdown syntax and vimtex syntax, more specifically

  1. use default markdown syntax in non-math zones
  2. take advantage of vimtex's math cancellation in math zones

So I create ~/.config/nvim/syntax/markdown.vim like the following:

" Vim syntax file

if exists('b:current_syntax')
  finish
endif

" Include tex math in markdown
syn include @tex syntax/tex.vim
syn region mkdMath start="\\\@<!\$" end="\$" skip="\\\$" contains=@tex keepend
syn region mkdMath start="\\\@<!\$\$" end="\$\$" skip="\\\$" contains=@tex keepend

let b:current_syntax = 'markdown'

and it works great, now I have math cancellation in makrdown math zones:

image

However, as you see many other syntax elements, e.g. the header is not highlighted because I do not define them in the syntax file, so I source the nvim's default markdown syntax file in my custom syntax file:

if exists('b:current_syntax') || exists('b:_loaded_markdown_syntax')
  finish
endif

let b:_loaded_markdown_syntax = 1
runtime! syntax/markdown.vim

" Include tex math in markdown
syn include @tex syntax/tex.vim
syn region mkdMath start="\\\@<!\$" end="\$" skip="\\\$" contains=@tex keepend
syn region mkdMath start="\\\@<!\$\$" end="\$\$" skip="\\\$" contains=@tex keepend

let b:current_syntax = 'markdown'

Now I have syntax highlights from nvim's default syntax file, but the math zone isn't concealed anymore:

image


So I wonder where I did wrong and how I can have default markdown highlighting combined with vimtex's math conceal feature using a custom markdown syntax file? Thanks in advance!

Steps to reproduce

No response

Expected behavior

No response

Actual behavior

No response

Do you use a latexmkrc file?

No

VimtexInfo

Cannot use `:VimtexInfo` in markdown files.
@Bekaboo Bekaboo added the bug label Jan 26, 2024
@lervag
Copy link
Owner

lervag commented Jan 26, 2024

I believe the problem is quite simple. You forgot to unlet b:current_syntax. This should work, I think:

if exists('b:current_syntax')
  finish
endif

runtime! syntax/markdown.vim
unlet b:current_syntax

" Include tex math in markdown
syn include @tex syntax/tex.vim
syn region mkdMath start="\\\@<!\$" end="\$" skip="\\\$" contains=@tex keepend
syn region mkdMath start="\\\@<!\$\$" end="\$\$" skip="\\\$" contains=@tex keepend

let b:current_syntax = 'markdown'

@lervag lervag closed this as completed Jan 26, 2024
@Bekaboo
Copy link
Author

Bekaboo commented Jan 26, 2024

@lervag That does not work, also the b:_loaded_markdown_syntax is needed to avoid the endless recursive of sourcing syntax/markdown.vim files, so this is the content in my current ~/.config/nvim/syntax/markdown.vim:

if exists('b:current_syntax') || exists('b:_loaded_markdown_syntax')
  finish
endif

let b:_loaded_markdown_syntax = 1
runtime! syntax/markdown.vim
unlet b:current_syntax

" Include tex math in markdown
syn include @tex syntax/tex.vim
syn region mkdMath start="\\\@<!\$" end="\$" skip="\\\$" contains=@tex keepend
syn region mkdMath start="\\\@<!\$\$" end="\$\$" skip="\\\$" contains=@tex keepend

let b:current_syntax = 'markdown'

and this is what I get -- all highlighting in markdown math zone disappears (e.g. it does not make a difference before/after adding unlet b:current_syntax). Can you repro? Thanks!

image

@Bekaboo
Copy link
Author

Bekaboo commented Jan 26, 2024

I've also tried moving the syntax file to the after/syntax directory with only the following content to avoid sourcing nvim's default markdown syntax file manually:

syn include @tex syntax/tex.vim
syn region mkdMath start="\\\@<!\$" end="\$" skip="\\\$" contains=@tex keepend
syn region mkdMath start="\\\@<!\$\$" end="\$\$" skip="\\\$" contains=@tex keepend

but that does not give me math conceal in mathzones. Don't know why but the conceal works only if

  1. the tex syntax is included in syntax/markdown.vim, not after/syntax/markdown.vim, and
  2. the default markdown syntax file must not be sourced in syntax/markdown.vim

So it seems that the math concealing works only if nvim does not source the default markdown syntax file. I'm confused.

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

No branches or pull requests

2 participants