lervag / vimtex

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

rich delimiter toggling #229

Closed nilsbecker closed 8 years ago

nilsbecker commented 8 years ago

i like the tsd command. i think it addresses a common need when tuning formulas. however, personally i find the \left \right autosizing commands often choose a somewhat too big size. so i would warmly welcome a variation of tsd which would toggle through the list <nothing> \big \Big \bigg \Bigg in front of the surrounding delimiter pair. probably not everyone agrees, so it's not sure that this should replace the current tsd. but in principle it could, then one would use the list <nothing> \left \bigl \Bigl etc.. for the left delimiter.

side note: for me tsd is a little laggy?

lervag commented 8 years ago

Hi! Thanks for the suggestion. I think this seems like a valid feature, and I will implement it when I get the time. However, I think I will prioritize some of the other issues first, and as you see, I am lagging a little bit behind. Life has been busy, lately, so I am not sure when I get the time to do this. :\

lervag commented 8 years ago

So, I've finally implemented this. I added two new options that are set so that the current behaviour is not changed. To get the desired behaviour, you can set the options as follows:

let g:vimtex_change_toggled_delims = [['\\left', '\\right'], '\\big', '\\Bigg', ...]
let g:vimtex_change_ignored_delims_pattern = ''
lervag commented 8 years ago

I also added brief descriptions in the docs. Comments and feedback is appreciated.

nilsbecker commented 8 years ago

@lervag thanks a lot for implementing this. i tried it out just now. unfortunately it's not working for me with the settings you give above. in an equation within \left ... \right, nothing is toggled by typing tsd.

actually, if i do not change g:vimtex_change_ignored_delims_pattern, something does work but then i get clobbered nested delimiters on toggling through.

also, i had forgotten that you gave the list above. from the documentation, i then tried the list let g:vimtex_change_toggled_delims = [['\\left', '\\right'], ['\\bigl', '\\bigr'], ...] by analogy. it would be good if the documentation could mention the shorter format.

as a side note, it did not become clear to me from the docs what vimtex_change_ignored_delims_pattern is good for and why it's set to \bigg... .

lervag commented 8 years ago

\left ... \right should not be toggleable, since \left and \right are not commands in themselves. Instead, \left( ... \right) and similar is toggleable.

What happens if you try tsd with default options inside \left( test \right)?

I actually can't remember why I needed vimtex_change_ignored_delims_pattern. I guess there was a reason...

Btw, I see that \\bigl and \\bigr will not be detected because they are not detected by the function that gets the surrounding delimiters. What is the difference between \bigl and simply \big?

nilsbecker commented 8 years ago

sorry, i was imprecise. i did try it inside \left( ... \right) .

anyway, with the default options,

\[
    \left( xyz \right)
\]

does toggle correctly between ( and \left( and vice versa. it also works with [,\{. good.

now if i set vimtex_change_ignored_delims_pattern='' and change nothing else, toggling already does not work. so i leave that unchanged at the default (mysterious) value.

next, i change let g:vimtex_change_toggled_delims = [['\\left', '\\right'], '\\big', '\\Big'] but this toggles only once to the form \big( xyz \big) and then gets stuck there. this format is actually not what's needed: for left and right delimiters, \bigl, \bigr are required. i think it's to mark that these are really corresponding delimiters, but i'm not sure - maybe it has an effect on global spacings? also, for alternatives sometimes one uses an empty righthand side. so, x = \left\{ \begin{aligned} 1& y>0 \\ 0 & \text{ otherwise} \end{aligned} \right. works, and it also works if using \Bigl\{ .. \Bigr.. in that case, the explicit l and r would indicate that the delimiters actually correspond to each other. maybe that's the general rule? another use of non-matching delimiters: a ket in quantum mechanics, \left| \lambda \right\rangle .

the only uses i've seen for \big are when there is no matching bracket. e.g. \bigl\langle x \big| y \bigr\rangle -- this makes me notice that \langle \rangle should also work as brackets for toggling (i haven't tried). another use is f(x)\Big|_{x=0} .

finally, i set the list to let g:vimtex_change_toggled_delims = [['\\left', '\\right'], ['\\bigl', '\\bigr']] but this produces nesting delimiters on repeated use when started from ( xyz )

nilsbecker commented 8 years ago

ok, so i think the case of non-corresponding left and right delimiters it would not be possible to toggle to and from the normal text-size delimiter, since then one does not know what even is the delimiter pair in question.

for a solution in full glory one would then need one toggle from text-size, bare delimiters to \left and \right and back -- this would work as it is now. then a separate toggle would rotate around the different sizes of fancy delimiters. that would not go back to text size, but would decide on the delimiter pair only by looking at the \left.., \bigl... etc prefixes. this would allow to have non-matching delims.

although it may be overkill, this solution may actually be cleaner? disadvantage is of course, one more command. for me, either solution would be satisfactory: one command which requires matching delimiters and toggles through everything, or the above idea with two separate commands.

nilsbecker commented 8 years ago

another idea i just stumbled upon:

if it was possible to use csd for change surrounding delimiter, and then type e.g. \left[ or \Bigl( that would also be completely good enough. it's only a little more verbose than toggling and could be cleaner in that it requires much less configuration etc.

lervag commented 8 years ago

I just wanted to mention that there are currently more issues regarding delimiters (e.g. #314). I think I need to update vimtex in a more general way for delimiters, as there are currently several redundent definitions of opening and closing delimiters in the code. I want to generalize this before I solve this current issue. I think this issue should become easier to solve when the delimiters are defined in a more consice manner.

Thanks for the suggestions, though. I've tried to create a list of todos to make things more clear for future reference:

Note that I am not really sure if I will be able to support the nonmatching delimiters. I'll have a go at it, though.

lervag commented 8 years ago

I've just pushed a huge commit that fixes a lot of issues with the delimiter handling. I removed the confusing option, as it is no longer needed.

I think there are a lot of things that could be improved or added still. I'd be happy to continue the work, but it would be easier if you could help by opening new issues with simple and clear feature requests for the features you think should be added (e.g. if you think it is useful with a mapping to change the current delimiter, or support for the . delimiter (useful with the modifiers).

nilsbecker commented 8 years ago

ok thanks, i'll check it out!