SirVer / ultisnips

UltiSnips - The ultimate snippet solution for Vim. Send pull requests to SirVer/ultisnips!
GNU General Public License v3.0
7.51k stars 690 forks source link

Replacement string does not display backslash and the following letter #1495

Open celdorwow opened 1 year ago

celdorwow commented 1 year ago

Expected behavior: \begin{tabularx}{\linewidth}{cc} X & X \\ \end{tabularx}

Actual behavior: \begin{tabularx}{inewidth}{cc} X & X \\ \end{tabularx}

Steps to reproduce

  1. Create a snippet

    snippet tabu "Regular tabular" b
    \\begin{tabular$1}${1/(\*)$|(x)$/(?1:{\\\linewidth})(?2:{\\\linewidth})/}{${2:cc}}
    $0X & X \\\\
    \\end{tabular$1}
    endsnippet
  2. Write tabu in a LaTeX file and press <TAB>

  3. The snippet displays \begin{tabular}{cc} and holds on position after r

  4. Appending * or x inserts {inewidth} in the middle but it should insert \linewidth.

As a comment, it doesn't matter how many times I escape \ in front of linewidth in the snippet

${1/(\*)$|(x)$/(?1:{\\linewidth})(?2:{\\linewidth})/}

or

${1/(\*)$|(x)$/(?1:{\\\linewidth})(?2:{\\\linewidth})/}

or

${1/(\*)$|(x)$/(?1:{\\\\linewidth})(?2:{\\\\linewidth})/}

the effect is the same.


celdorwow commented 1 year ago

I solved it using a Python interpolation:

snippet tabu "Regular tabular" b
\\begin{tabular$1}`!p if t[1] == "x" or t[1] == "*": snip.rv=r"{\linewidth}"`{${2:cc}}
    $0X & X \\\\
\\end{tabular$1}
endsnippet

However, I am going to leave it open because I don't know if what happens with the transformation above is a bug or intended.