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

Toggle further math environments #2310

Closed
clason opened this issue Jan 28, 2022 · 12 comments
Closed

Toggle further math environments #2310

clason opened this issue Jan 28, 2022 · 12 comments

Comments

@clason
Copy link
Contributor

clason commented Jan 28, 2022

(This is a follow-up to #1858.)

There are further pairs of math environments that would be useful to be able to toggle easily between them. (Some of them are already covered by cs$, but a toggle would be more convenient.)

  • $$ ... $$ -> \[ ... \] (one-way, since since $$ should not be used in LaTeX! note that ts$ currently errors in this situation)
  • \[ ... \] <-> \begin{equation*} ... \end{equation*} (here the direction -> is already possible via cs$, but <- is not).

It's not clear to me whether this would be better included as a three-way toggle, or a second toggle (tsm, for "toggle surrounding math"?).

lervag added a commit that referenced this issue Jan 28, 2022
@lervag
Copy link
Owner

lervag commented Jan 28, 2022

I believe I have fixed the issue with cs$ - it should now work with math envs. The list of environments is hard coded and may need to be expanded. And it will currently not work with nested environments. This may be improved upon, of course.

lervag added a commit that referenced this issue Jan 28, 2022
@lervag
Copy link
Owner

lervag commented Jan 28, 2022

I believe the current code should be easy to understand:

function! vimtex#env#toggle_math() abort " {{{1
let [l:open, l:close] = vimtex#env#get_surrounding('math')
if empty(l:open) | return | endif
let l:map = {
\ '$': '\[',
\ '\[': '$',
\ '$$': '\[',
\ '\(': '$',
\}
call vimtex#env#change(l:open, l:close, get(l:map, l:open.match, '$'))
endfunction

Currently, the only toggle loop is between $ and \[. Do we want to make this more advanced, or is this the most "expected" behaviour? Remember, cs$ should now work as expected and allows to make an adjusted version here.

I'm thinking that it could be interesting to have the loop be three-way, i.e.:

$...$  ->  \[ ... \]  ->  \begin{equation} ... \end{equation}  ->  $ ... $

@clason
Copy link
Contributor Author

clason commented Jan 28, 2022

I think that's a very good compromise. (Although the \( -> $ transformation is a bit opinionated; the "official upgrade" would be the other way around ;))

The three-way loop is also attractive (and I now agree it should be without *), although I understand if people found it annoying to skip the unwanted alternative. If it allowed code simplification, however, I'd be all for it!

lervag added a commit that referenced this issue Jan 28, 2022
@lervag
Copy link
Owner

lervag commented Jan 28, 2022

Ok. How do you think the updated behaviour is? Does it resolve your issues?

Although the \( -> $ transformation is a bit opinionated; the "official upgrade" would be the other way around

Good point. I personally have never become used to the \(...\) syntax and I more or less never see it anywhere. But you are probably right that it is officially considered an upgrade. Still, let's be pragmatic. If anyone should complain I'll be glad to reconsider!

@lervag lervag closed this as completed Jan 28, 2022
@clason
Copy link
Contributor Author

clason commented Jan 28, 2022

I think it's quite nice, yes!

(And personally I agree on the syntax; it's just too painful to type. I just mentioned this for the sake of being pedantic :))

@vneiger
Copy link

vneiger commented Jan 28, 2022

The changes look good on my side as well. Thanks for the work on both issues!!

Just an off-topic comment: I don't think the syntax \(...\) is more painful than another. I discovered snippets thanks to the vimtex documentation, and for example using Ultisnips with something like

snippet $ "math \( ... \)" i
\(${1:${VISUAL}}\)$0
endsnippet

makes it as convenient to use \(...\) as the more classical $...$.

@lervag
Copy link
Owner

lervag commented Jan 28, 2022

Yes, that's a good point, @vneiger. I believe my "problem" with \(...\) is that I don't need it. That is, there's nothing wrong with $...$ for me - it works as expected and is easy to type.

@xsrvmy
Copy link

xsrvmy commented Jan 31, 2022

On the topic of \(\) and $...$, the first syntax is actually better in some ways because it makes syntax highlighting and conceal changes more contained if you type a backslash at the end of math mode.
Is it possible to expose that list to configuration by any chance?

@lervag
Copy link
Owner

lervag commented Jan 31, 2022

Is it possible to expose that list to configuration by any chance?

Sure. I've pushed an update that exposes it through :help g:vimtex_env_toggle_math_map.

lervag added a commit that referenced this issue Jan 31, 2022
@xsrvmy
Copy link

xsrvmy commented Jan 31, 2022

Actually there is another bug here: going from \( to \[ will not add new lines like going from $ to \[.

Also sorry for the number of comments recently. I should probably fork this repo at some point and send PRs instead.

@lervag
Copy link
Owner

lervag commented Jan 31, 2022

Actually there is another bug here: going from \( to \[ will not add new lines like going from $ to \[.

Ah, yes - sorry, and good catch. I'll fix it.

Also sorry for the number of comments recently. I should probably fork this repo at some point and send PRs instead.

No problem, I enjoy thoughtful and good comments. So far I find yours to be useful, although not always so easy to address. :)

lervag added a commit that referenced this issue Jan 31, 2022
@lervag
Copy link
Owner

lervag commented Jan 31, 2022

(But obviously, PR's are more than welcome!)

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

4 participants