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

Adding custom user-defined text commands inside math environments (like align) to be recognized #2224

Closed
kwand opened this issue Oct 27, 2021 · 5 comments
Labels

Comments

@kwand
Copy link

kwand commented Oct 27, 2021

Description

Apologies for opening up an issue for this, but I've been looking around for a while and tried various things in the docs/mentioned in issues here, but couldn't seem to be able to find a solution to this. (I don't really understand vimscript either, so also apologies if I'm missing an obvious solution here.)

I have a custom command \mathnote{...} (lifted from TeX StackExchange, which is basically a \marginnote{...} command, but works inside math environments like align) which I want to be treated like \text{...}, so it syntactically looks proper and, most importantly, doesn't cause my auto-expanding snippets to trigger while typing text.

My current workaround is pretty crude: I've basically just modified the core.vim file to recognize 'mathnote':

" Text Inside Math regions
for l:re_cmd in [
      \ 'text%(normal|rm|up|tt|sf|sc)?',
      \ 'intertext',
      \ '[mf]box',
      \ 'mathnote',
      \]
  execute 'syntax match texMathCmdText'
        \ '"\v\\' . l:re_cmd . '>"'
        \ 'contained skipwhite nextgroup=texMathTextArg'
endfor
call vimtex#syntax#core#new_arg('texMathTextArg')

This, of course, doesn't really work well when you need to update VimTex.

I suspect there is a way to do this via the vimrc or some sort of custom tex.vim file that I can place in my dotfiles? (Or at least something better than hacking the plugin files)

@kwand kwand added the bug label Oct 27, 2021
@kwand
Copy link
Author

kwand commented Oct 27, 2021

Also, apologies for the 'bug' label. Looks like I opened the wrong type of issue.

@lervag
Copy link
Owner

lervag commented Oct 29, 2021

There's the option g:vimtex_syntax_custom_cmds which is quite close. I could image something like this:

let g:vimtex_syntax_custom_cmds = [
      \ {'name': 'mathnote', 'mathmode': 1, 'nextgroup': 'texMathTextArg'},
      \]

This should create the group texMathCmdCMathnote that matches \mathnote and continuous to match the {...} as texMathTextArg.

lervag added a commit that referenced this issue Oct 29, 2021
@lervag
Copy link
Owner

lervag commented Oct 29, 2021

I've implemented this. I believe you can get your desired functionality with my prevoiusly listed configuration option.

@lervag lervag closed this as completed Oct 29, 2021
@kwand
Copy link
Author

kwand commented Oct 30, 2021

Awesome! It works perfectly. Thanks!

@lervag
Copy link
Owner

lervag commented Oct 30, 2021

Glad to hear it :)

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