lervag / vimtex

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

vimtex to detect custom math environments #2499

Closed Alexandre867 closed 2 years ago

Alexandre867 commented 2 years ago

Is it possible to have vimtex detect custom math environment? It's causing me issues when using context-dependant snippets.

I found out I can fix it by manually adding every custom environment using call vimtex#syntax#core#new_region_math('nsflalign') for example in my ftplugin/tex.vim.

Here's a .tex example, with how it looks without and with adding the custom environment manually.

\documentclass{article}
\usepackage[paperheight=2in,paperwidth=3in,margin=.25in]{geometry}
\usepackage{mathtools}
\newenvironment{nsflalign}{\setlength{\abovedisplayskip}{0pt}\setlength{\belowdisplayskip}{0pt}\csname flalign\endcsname}{\csname endflalign\endcsname\ignorespacesafterend}

\begin{document}

\begin{nsflalign}
    1+2 &= 3 &\\
    \mu^2+\tau^2 &= \theta^2+4
\end{nsflalign}

\begin{flalign}
    1+2 &= 3 &\\
    \mu^2+\tau^2 &= \theta^2+4
\end{flalign}

\end{document}

Screenshot of the code without fix Screenshot of the document Screenshot of the code with manual fix

lervag commented 2 years ago

I found out I can fix it by manually adding every custom environment using call vimtex#syntax#core#new_region_math('nsflalign') for example in my ftplugin/tex.vim.

This is precisely how you should handle this specific case.

Is it possible to have vimtex detect custom math environment?

No, that's too hard. Sorry!

haoming-li-ling commented 2 years ago

Do you have any clue why putting that line in my ftplugin/tex.vim does not work, but typing it in the command line after a tex file is loaded works?

lervag commented 2 years ago

Thanks for that questions - it shows that I was too quick in my previous reply!

You need to execute that line after the VimTeX syntax scripts have executed. Putting the line in your personal ftplugin/tex.vim does not ensure this. A more appropriate place to put this (and similar) customizations would be in your after/syntax/tex.vim, because this script will be executed after all the main syntax scripts.

haoming-li-ling commented 2 years ago

That works! I have another question: is there a way to disable conceal in certain environments?

lervag commented 2 years ago

Partly; you can control the conceal options with :help g:vimtex_syntax_conceal. It is not very customizable, sorry.

It would be easier to answer a concrete question, though...

haoming-li-ling commented 1 year ago

For example, I want math-mode highlighting but not math-mode conceal inside the environment tikzcd, since I want to be able to align by & (tikzcd turns on math mode on the inside and it is fundamentally a tabular-like environment); I added it to the list of math env delimiters for the former purpose, but then the math symbols are concealed, making manual alignment useless.

lervag commented 1 year ago

Yes, that sounds like an understandable thing to want.

However, I am sorry to say it is currently not possible. Implementing support for this would be a lot of work, and I don't really have the time for it. I would need to change some core ideas for the syntax highlighting of math zones.

I personally don't like conceals at all, so I have them disabled. I think you will need to consider that as an option.