lervag / vimtex

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

Help with custom conceal using regex #2795

Closed userrand closed 1 year ago

userrand commented 1 year ago

Description

The following command works in a vimrc file without vimtex

syntax match TexMathSymbol '\%(\\frac\)\@<={\%([^}]\{-}}{[^}]\{-}}\)\@='  conceal cchar=❬

But I cannot get it to work with vimtex in neovim. I tried the following, (According to the documentation, it seems the regex is expected to be in very magic mode)

let g:tex_conceal='abdmg'
set concealcursor=nvic
set conceallevel=2
let g:vimtex_syntax_custom_cmds = [
      \ {'name': 'frac', 'cmdre': '(\\frac)@<={([^}]{-}}{[^}]{-}})@='    , 'mathmode': 1, 'concealchar': '❬'},
      \]

Steps to reproduce

No response

Expected behavior

No response

Actual behavior

No response

Do you use a latexmkrc file?

No

VimtexInfo

System info:
  OS: Linux 6.4.12-100.fc37.x86_64
  Vim version: NVIM v0.9.1
  Has clientserver: true
  Servername: /run/user/1000/nvim.113725.0

VimTeX project: test
  base: test.tex
  root: /home/claude/projects
  tex: /home/claude/projects/test.tex
  main parser: current file verified
  document class: article
  packages: algorithm algorithmic amsfonts amsmath amssymb amsthm babel bbm bm cancel caption cite color enumitem epsfig fontenc footmisc graphicx helvet inputenc lmodern setspace soul subfigure textcomp titlesec url verbatim wasysym wrapfig
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: General
  qf method: LaTeX logfile
lervag commented 1 year ago

The following command works in a vimrc file without vimtex

syntax match TexMathSymbol '\%(\\frac\)\@<={\%([^}]\{-}}{[^}]\{-}}\)\@='  conceal cchar=❬

But I cannot get it to work with vimtex in neovim. I tried the following, (According to the documentation, it seems the regex is expected to be in very magic mode)

let g:tex_conceal='abdmg'
set concealcursor=nvic
set conceallevel=2
let g:vimtex_syntax_custom_cmds = [
      \ {'name': 'frac', 'cmdre': '(\\frac)@<={([^}]{-}}{[^}]{-}})@='    , 'mathmode': 1, 'concealchar': '❬'},
      \]

A couple of initial comments:

The g:vimtex_syntax_custom_cmds is probably too specific for your need, because it is meant to match the command. You are using an advanced regex to ignore the initial \frac part. Notice that cmdre does not include the initial \ and that it will only match on the command name itself - you don't match on the following argument groups.

It seems to me that you want a very specific customization of your \fracs, and I would advice to instead use an after/syntax/tex.vim file to specify your rules on top of VimTeX. Your existing rules will work, but you should make some adjustments:

syntax match texMathSymbol contained '\%(\\frac\)\@<={\%([^}]\{-}}{[^}]\{-}}\)\@=' conceal cchar=❬

I.e.: use texMathSymbol as the group name (with lower t) and add contained to avoid having it match outside mathmode.

userrand commented 1 year ago

Thank you very much for your general help given in the comments and the help with syntax. I am using neovim (specifically Astronvim), what would be the equivalent of after/syntax/tex.vim in that case ?

lervag commented 1 year ago

Sorry, I don't know. I don't use AstroNvim. But for neovim, after/syntax/tex.vim works perfectly fine. (i.e. ~/.config/nvim/after/syntax/tex.vim)

userrand commented 1 year ago

I tried your suggestion to use

syntax match texMathSymbol contained '\%(\\frac\)\@<={\%([^}]\{-}}{[^}]\{-}}\)\@=' conceal cchar=❬

and add it to ~/.config/nvim/after/syntax/tex.vim but that did not work.

Is it possible to use g:vimtex_syntax_custom_cmds for that? In the worst-case scenario where i decide to turn off vim conceal and reimplement all of the conceals relevant to my use case, how can i deactivate the conceals of vimtex such that it no longer disables custom conceals?

lervag commented 1 year ago

No, you can't use g:vimtex_syntax_custom_cmds for this. You can deactivate conceals with let g:vimtex_syntax_conceal_disable = 1.

userrand commented 1 year ago

Seems like I made a mistake somewhere when I tried your suggestion and my issue was mainly that I did not place the conceal code in ~/.config/nvim/after/syntax/tex.vim. The code works in the way it was originally written in that location.

My apologies the issue might not have been related to vimtex. Thank you for your time and help.

lervag commented 1 year ago

Seems like I made a mistake somewhere when I tried your suggestion and my issue was mainly that I did not place the conceal code in ~/.config/nvim/after/syntax/tex.vim.

We all make mistakes :)

The code works in the way it was originally written in that location.

My apologies the issue might not have been related to vimtex. Thank you for your time and help.

No problem, glad to hear it works as expected now!