since moving from emacs/auctex to VScode/LaTeX-Workshop I'm only missing a small but very comfortable feature.
In math-mode if you type ^ or _ for super/subscript curly brackets where automatically added and the cursor placed inside (for super/subscripts with more than one character).
Is it possible to get this behavior in LaTeX-Workshop as well?
Unfortunately, it's not possible for the extension to detect math-mode. An alternative is to provide an intellisense item upon typing ^ or _, which is however can be disturbing in non-math-mode.
Discussions welcomed.
I have a few extra snippets for such really cool features.
First, ^ is a very tricky key for snippets as in most keyboard layouts, it is a dead key. To avoid interference with non math mode, I use the vim-latex approach, which is to trigger the snippet for _ _ (double _) and ^^. To circumvent the dead key trouble, I actually have to type ^ + space twice.
@jlelong how do these snippets look like?
(sorry but I'm really just starting with VSCode and I'm not a real programmer)
a behavior like double ^ opens up a curly bracket would be sufficient for me.
also if the brackets would appear in non-math mode wouldn't be a problem if there is an easy solution for that.
@jlelong I am also very curious how the snippet would look like.
@James-Yu I don't believe latex allow _ or ^ outside of math mode.
_ or ^ outside of mathe modes will raise a missing $ inserted error.
Therefore I believe when people trying to type _ or ^ (without backslash \ in front of them),
we can safely assume that they are in math mode.
In the menu _Preferences / User Snippets_, I choose the latex and simply added
"subtext" : {
"prefix": "__",
"body": "_{$1}",
"description": "Add a subtext"
},
"supertext" : {
"prefix": "^^",
"body": "^{$1}",
"description": "Add a supertext"
},
To use the snippet, just enter _ twice and hit TAB.
@James-Yu, I could make a PR (one more 馃槣)
@chantisnake, _ can appear on its own without a leading \ outside math mode if one uses the underscore package.
@jlelong nice snippet, unfortunately, for this snippet you need a space in front of the ^ or _ to work.
for example
\(G__<tab>\)
will not expand the snippet.
People will have to type
\(G __<tab>\)
to trigger this snippet.
@James-Yu I think you maybe able to detect whether the cursor is in mathmode using the vscode-textmate module.
I don't know whether this will significantly slow down the performance.
You do not need a leading space. When using \( ... \), you actually nest snippets. Hitting TAB after __ does not trigger that snippet but is rather interpreted by the first one and makes you jump after \).
@jlelong
I added your snippets to my latex.json
the subtext snippet works like expected, the supertext not.
It seems like the ^ key is not triggering anything...
I'm using german and english keyboard layout without dead keys. Do you have some idea how to solve that?
anyhow, thank you a lot for that simple trick, most of the time I'm typing longer subscripts; superscripts are not too important.
@maaaaadn, have you tried typing ^^SPACE and then TAB. When I was using Vim, I never managed to make ^^ trigger anything. You could use any other key, maybe ** would be a could alternative...
@James-Yu, I cannot figure out how to integrate them to LaTeX-Workshop. My problem is that typing __ does not pop up the intellisense menu as it should but hitting TAB properly expands the snippet. Whereas, I thought it did not matter, it seems to be related to the problem of nesting a classic snippet inside an other one triggered by \ (see @chantisnake's remark).
Nesting two snippets triggered by \ is fine

But, nesting a standard snippet inside a \ triggered one fails

Hitting TAB makes you jump off the } instead of expanding the __ snippet.
@jlelong unfortunately but also as expected I need to add a space before using the snippet.
otherwise it's not triggered.
I'll try it out today if it is faster than typing the brackets manually...
thank you a lot for the quick effort!
After playing around with all the snippets, I figured it out that if the \(...\) was defined as
(${0}\\) instead of (${1}\\)${0}, the following snippets
"subtext" : {
"prefix": "__",
"body": "_{$0}",
"description": "Add a subtext"
},
"supertext" : {
"prefix": "**",
"body": "^{$0}",
"description": "Add a supertext"
}
would work as expected. In particular \(G__<tab>\) would expand correctly.
I think, it may be useful to modify some snippets to use $0 as the placeholder for the _body part_. I would suggest to do it for
begin...end (Note that inserting environment using \envnane already uses $0 for the body part).\left(...\right), \left\{...\right\}, \left[...\right]\(...\), \{...\}, \[...\]I am waiting for your comments. @James-Yu ?
@jlelong I totally agree with you. Do you prefer me making the changes or PR? Either fine for me.
I already have the modifications, so I can make the PR right away.
Would that make sense to add the __ and ** snippets directly to the repo through a snippets directory with the idea of maybe introducing some more snippets not starting with \ in the future?
I am leaving it as an open question for now, waiting for comments.
I like the idea quite a lot. In fact I myself is an amateur LaTeXer (n00b to be exact), so the extension really relies on the community to bring up ideas and data 馃槩
Actually, many of the snippets I use are inspired by vim-latex which I was using for many years before switching to vscode. To summarize
BXY inserts an environment with XY being the first two letters of the environment name.BSXY does the same as above but using the star version of the environment.@ + letter inserts the associated greek letter. @a expands into \alpha.FXY inserts the font modifier \textxy where xy can be it, bf, tt or em.@( inserts \left(...\right). Works also for { and [.Some of the snippets are redundant with already existing ones starting with \, but the \ letter requires three simultaneous keystrokes of a French Apple keyboard!
I propose to make a PR with all these snippets and use comment on it to discuss the relevance of all this.
I suggest to continue the discussion by commenting on #618
Thank you so much @jlelong.
thanks a lot.
and I would like the idea of a snippets folder.
as far as I'm really into vscode I'll prepare some snippets for myself as well which I would be happy to share with you :+1:
I think the request is very valid but the solution needs some improvement
Most helpful comment
In the menu _Preferences / User Snippets_, I choose the
latexand simply addedTo use the snippet, just enter
_twice and hitTAB.@James-Yu, I could make a PR (one more 馃槣)
@chantisnake,
_can appear on its own without a leading\outside math mode if one uses theunderscorepackage.