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

[Syntax Conceal] Conceal completely \hphantom{arg} #2642

Closed
kiryph opened this issue Feb 23, 2023 · 10 comments
Closed

[Syntax Conceal] Conceal completely \hphantom{arg} #2642

kiryph opened this issue Feb 23, 2023 · 10 comments

Comments

@kiryph
Copy link
Contributor

kiryph commented Feb 23, 2023

Is it possible to conceal \hphantom{arg} completely?

@kiryph
Copy link
Contributor Author

kiryph commented Feb 23, 2023

I have created for neovim the file ~/.config/nvim/after/syntax/tex.vim with the content:

syntax match texCmd     "\\hphantom{.\{-}} \?" conceal
syntax match texMathCmd "\\hphantom{.\{-}} \?" contained conceal

However, this has problems when nesting other commands, e.g. \hphantom{\mathbin{-}} is not correctly concealed. A closing } is visible. One level can be allowed by following

syntax match texCmd     "\\hphantom{[^{}]*\({[^{}]*}[^{}]*\)*} \?" conceal
syntax match texMathCmd "\\hphantom{[^{}]*\({[^{}]*}[^{}]*\)*} \?" contained conceal

Another level is already nasty:

syntax match texCmd     "\v\\hphantom\{[^{}]*(\{[^{}]*\}[^{}]*)*(\{[^{}]*(\{[^{}]*\}[^{}]*)*\}[^{}]*(\{[^{}]]*\}[^{}]*)*)*\} ?" conceal
syntax match texMathCmd "\v\\hphantom\{[^{}]*(\{[^{}]*\}[^{}]*)*(\{[^{}]*(\{[^{}]*\}[^{}]*)*\}[^{}]*(\{[^{}]]*\}[^{}]*)*)*\} ?" contained conceal

Credit goes to https://vi.stackexchange.com/a/38259/1292 for providing the expression for nested square brackets.

@lervag
Copy link
Owner

lervag commented Feb 24, 2023

The following code works for me (you could put it in after/syntax/tex.vim, for instance):

syntax match texCmdPhantom "\\hphantom\>"
      \ conceal skipwhite nextgroup=texPhantomArg
syntax match texMathCmdPhantom "\\hphantom\>"
      \ conceal skipwhite nextgroup=texPhantomArg
syntax cluster texClusterMath add=texMathCmdPhantom

call vimtex#syntax#core#new_arg('texPhantomArg', {
      \ 'opts': 'contained conceal',
      \ 'contains': 'texPhantomGroups',
      \})
call vimtex#syntax#core#new_arg('texPhantomGroups', {
      \ 'matchgroup': 'matchgroup=NONE',
      \ 'opts': 'contained conceal',
      \ 'contains': 'texPhantomGroups',
      \})

highlight def link texCmdPhantom       texCmd
highlight def link texPhantomArg       texArg
highlight def link texPhantomGroups    texPhantomArg

I could add this to VimTeX as a feature - do you think that would be useful? Should it be enabled by default with a flag in g:vimtex_syntax_conceal?

@lervag
Copy link
Owner

lervag commented Feb 24, 2023

By the way: a core idea to make this work is to use syntax region to match the brace groups. That way we don't have to use regexps to match nested levels (reg exps are generally not suitable for that type of parsing).

@kiryph
Copy link
Contributor Author

kiryph commented Feb 24, 2023

Thanks for all your feedback.

By the way: a core idea to make this work is to use syntax region to match the brace groups. That way we don't have to use regexps to match nested levels (reg exps are generally not suitable for that type of parsing).

It is great that there is a proper solution to this and I totally agree that reg exps are here not the right tool.

I could add this to VimTeX as a feature - do you think that would be useful? Should it be enabled by default with a flag in g:vimtex_syntax_conceal?

Obviously, I find it useful.

The popular answer on https://tex.stackexchange.com/questions/74353/what-commands-are-there-for-horizontal-spacing lists quite a few commands to add horizontal spacing. \hphantom is listed there on position 13.

VimTeX already conceals a part from this list:

function! s:match_conceal_spacing() abort " {{{1
syntax match texSpecialChar "\\[,;:!]" conceal
syntax match texCmd '\\bigskip\>' conceal
syntax match texCmd '\\hfill\>' conceal
syntax match texCmd '\\medspace\>' conceal
syntax match texCmd '\\qquad\>' conceal
syntax match texCmd '\\quad\>' conceal
syntax match texCmd '\\thickspace\>' conceal
syntax match texCmd '\\thinspace\>' conceal
syntax match texCmd '\\vfill\>' conceal
syntax match texCmd "\\[hv]space\>" conceal
\ skipwhite nextgroup=texConcealedArg
syntax match texMathCmd '\\[,:;!]' contained conceal
syntax match texMathCmd '\\bigskip\>' contained conceal
syntax match texMathCmd '\\hfill\>' contained conceal
syntax match texMathCmd '\\medspace\>' contained conceal
syntax match texMathCmd '\\qquad\>' contained conceal
syntax match texMathCmd '\\quad\>' contained conceal
syntax match texMathCmd '\\thickspace\>' contained conceal
syntax match texMathCmd '\\thinspace\>' contained conceal
syntax match texMathCmd '\\vfill\>' contained conceal
syntax match texMathCmd "\\[hv]space\>" contained conceal
\ skipwhite nextgroup=texConcealedArg
call vimtex#syntax#core#new_arg('texConcealedArg', {
\ 'opts': 'keepend contained conceal concealends',
\})
endfunction

So one could argue that \hphantom fits into the group

" Conceal spacing commands
if g:vimtex_syntax_conceal.spacing
call s:match_conceal_spacing()
endif

Actually, I have added myself \> as well.

@kiryph
Copy link
Contributor Author

kiryph commented Feb 24, 2023

And I actually have added cchar= (space), since it usually introduces additional whitespace.

lervag added a commit that referenced this issue Feb 24, 2023
@lervag
Copy link
Owner

lervag commented Feb 24, 2023

The popular answer on https://tex.stackexchange.com/questions/74353/what-commands-are-there-for-horizontal-spacing lists quite a few commands to add horizontal spacing. \hphantom is listed there on position 13.

VimTeX already conceals a part from this list:

Ah, yes, I agree. I've pushed an update that adds conceals for \phantom and \hphantom now. Should we also add the cchar=? I guess its subjective if that's desireable, but I don't want to add an option for it... I'll just go with whatever you find best. :)

@lervag lervag closed this as completed Feb 24, 2023
@kiryph
Copy link
Contributor Author

kiryph commented Feb 24, 2023

Should we also add the cchar=? I guess its subjective if that's desireable, but I don't want to add an option for it... I'll just go with whatever you find best. :)

I see: delegating the blame/pressure 😅 .

I would go with what is done until now, i.e. no space. Do not change existing behaviour except someone comes with a good reason.

I agree that this is subjective. Concealment in general feels this ways since it is only an approximation and it depends on how one writes tex code and formats it. I actually have changed a few places how I format my code due to enabling concealment (e.g. no alignment of \\ in matrices since they appear at some arbitrary column).

@lervag
Copy link
Owner

lervag commented Feb 24, 2023

Should we also add the cchar=? I guess its subjective if that's desireable, but I don't want to add an option for it... I'll just go with whatever you find best. :)

I see: delegating the blame/pressure sweat_smile .

;)

I would go with what is done until now, i.e. no space. Do not change existing behaviour except someone comes with a good reason.

Sounds good!

I agree that this is subjective. Concealment in general feels this ways since it is only an approximation and it depends on how one writes tex code and formats it. I actually have changed a few places how I format my code due to enabling concealment (e.g. no alignment of \\ in matrices since they appear at some arbitrary column).

Yes. I personally don't like the conceals and don't use them.

@kiryph
Copy link
Contributor Author

kiryph commented Feb 24, 2023

I personally don't like the conceals and don't use them.

I am always impressed about the many features and quality of them you have added which you do not use yourself. Thank you for this.

@lervag
Copy link
Owner

lervag commented Feb 24, 2023

I find pleasure in trying to solve (sometimes hard) problems in a good way. As I'm no longer in academia, I also use LaTeX more seldom personally, but I still find it fun to tinker with VimTeX. :)

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

No branches or pull requests

2 participants