SirVer / ultisnips

UltiSnips - The ultimate snippet solution for Vim. Send pull requests to SirVer/ultisnips!
GNU General Public License v3.0
7.51k stars 690 forks source link

Detecting Math Mode in Markdown Files #1396

Open ayushxx7 opened 2 years ago

ayushxx7 commented 2 years ago

Expected behavior: Ultisnips should recognize $text inside dollar symbols for snippet expansion$ when using context math in markdown file

Actual behavior: It doesn't recognize math context

Steps to reproduce

  1. Add the following in snippets configuration file:
    
    global !p
    def math():
    return vim.eval('vimtex#syntax#in_mathzone()') == '1'
    endglobal

context "math()" snippet sq "\sqrt{}" iA \sqrt{${1:${VISUAL}}} $0 endsnippet



2. Open test.md and enter `$sq$` in the file

3. Failure to trigger snippet expansion

Expected behavior -> Expands to `$\sqrt {takes input here}$`
Actual behavior -> text remains as `$sq$`

-----
<!-- NOTE: contents inside arrows will be ignored. -->
- **Operating System**: Windows 10
- **Vim Version**: 
NVIM v0.6.0-dev+262-g643cb8a6e
Build type: RelWithDebInfo
- **UltiSnips Version**: 3.2 <!-- e.g. 3.1. If you're using version from git 
                              run: `git rev-parse origin/master` -->
- **Python inside Vim**: 3.9 <!-- e.g. 2.7.14 / 3.6.5. If unsure run inside vim:
                              `:py import sys; print(sys.version)` and
                              `:py3 import sys; print(sys.version)' -->
- **Docker repo/vimrc**: https://github.com/ayushxx7/dotfiles <!-- link to the branch containing the repro case, 
                              or the uploaded minimal vimrc -->

For now, I have removed context math() from the snippets, but it causes problems because I have to come up with snippet trigger words that won't be used in English otherwise and that makes it unintuitive to use at times.
jfab20 commented 2 years ago

I think vimtex is the one that recognizes math mode, so it is not ultisnips fault. Vimtex recognizes math mode with the use of syntax highlighting, you can see this if you set syntax off in a .tex file.

ayushxx7 commented 2 years ago

The file that I want it to be enabled in has .md extension.

jfab20 commented 2 years ago

If vimtex cannot provide syntax highlighting for that particular extension, then your python function is not going to work

AndreasHolt commented 2 years ago

The file that I want it to be enabled in has .md extension.

did you find a solution? struggling with the same problem rn

ayushxx7 commented 2 years ago

The file that I want it to be enabled in has .md extension.

did you find a solution? struggling with the same problem rn

No, I couldn't get it working. Hence, I came up with a "some-what-decent" workaround. Say I want to type \epsilon then I assign the shortcut to eee which is something I would never write in actual text.. similarly bbb for \beta.

Meaning I came up with shortcuts that I don't normally end up writing in text, while also making sure that it's not too hard for me to remember. It works for the most part.

@AndreasHolt

richyliu commented 2 years ago

If you want the snippet to work while you are editing a markdown file, add it to your markdown.snippets file or to all.snippets. Additionally, make sure g:markdown_fenced_languages includes "tex" (to get syntax highlighting). For example:

let g:markdown_fenced_languages = [tex']

When editing latex in markdown, use this syntax to get the correct syntax highlighting:

```tex
$2+2$ \text{hi}


Your example above works for me with these settings.
wookayin commented 2 years ago

I think the OP's code just works perfectly in markdown files (even when the file extension is *.md) if you use, for example, vim-pandoc. No further configuration was needed for me. FYI, I use https://github.com/vim-pandoc/vim-pandoc-syntax, which has embedded tex (math mode) syntax support.

image

iChunyu commented 2 years ago

Hello, I've just fixed the problem with the help of lervag's suggestion here.

The key operation for me is to install the vim-markdown and add the following configuration:

let g:vim_markdown_math = 1

Hope this solution fixes your problem too.

AadiWaghray commented 1 year ago

@iChunyu When I implemented the plugin, the default syntax highlighting for markdown through nvim was overwritten. Would you know how to undo this? Otherwise I found the plugin perfect for my use case.

iChunyu commented 1 year ago

@AadiWaghray Hi. I'm facing the same issue. Syntax highlighting (especially through treesitter) breaks vimtex#syntax#in_mathzone() and they seem not compatible by now. For me, I choose to disable the highlighting.

Sorry I can't provide any useful information.