1

I want the possibility of transforming an inline equation in latex to a display equation.

So I want $something$ to become

$$
something
$$

My attempt was this:

snippet "$(.+)$" "whatever" r
$$
\1
$$
$1
endsnippet

My understanding is that (.+) would match any character 1 or more times (captured in a group), and \1 would print the match in the group.

I also tried using python interpolation with !p snip.rv = match.group(1) to no success either.

Both attempt just lead to a tab inserted. Any ideas?

  • 1
    I’m not familiar, does ultisnips use vim flavored RegEx? If so + needs to be \+. – Jake Grossman Oct 15 at 23:28
  • Ok, I'm stupid..... The 2 "$" in the regex match condition shn ould be escaped But the \1 doesn't work, the python interpolation does.... – Ricardo Kullock Oct 16 at 2:03
  • shoot! Happens to the best of us. Welcome to Vi and Vim! – Jake Grossman Oct 16 at 2:04
  • Thanks! Should I delete the question? Seems like it was such a silly mistake (even though I was stuck on it for so long :/) – Ricardo Kullock Oct 16 at 2:05
  • If you have an answer, post it. If it’s just a typo that isnt likely to help others, we can close it, but to be honest I wouldn’t have thought of the escape necessarily, so I think it’s worth leaving. – D. Ben Knoble Oct 16 at 13:22
1

Because of Jake Grossman's comment, I eventually realized the $ in the regex trigger should be escaped: (Also had to use python interpolation)

snippet "\$(.+)\$" "whatever" r
$$
`!p snip.rv = match.group(1)`
$$
$0
endsnippet

The trigger right after snippet will match anything between $ and capture it in a group (with the ())

In the python interpolation, snip.rv stands for "return value" and the match.group(1) prints the first group in the regex (the only one in this case)

| improve this answer | |
  • 1
    I updated the snippet to use backquotes, I believe that's the correct syntax, no? Also, I think using $0 at the end (instead of $1) is slightly more correct, it indicates where the cursor goes after the snippet ends (rather than a field in the snippet.) – filbranden Oct 17 at 19:44
  • Did I use a different quote? The edited version in now exactly what I have in my config, so it should be correct, thanks.... I guess the $0 could be better, I'll check latter – Ricardo Kullock Oct 17 at 19:49
  • 2
    @RicardoKullock you can click "edited ..." to see the history; it looks like the original used ' instead of backticks – D. Ben Knoble Oct 17 at 21:06
  • 1
    Changed to use $0 like suggested – Ricardo Kullock Oct 17 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.