# Using Mathematica in Vim produces errors when printing in TeXForm (double backslash, “ b'-/n' ”)

I've been trying to integrate Mathematica with my Vimtex set-up, inspired by this article: https://castel.dev/post/lecture-notes-1/ The basic idea is to use UltiSnips to run an operation in Wolfram Script without leaving Vim, for quick computations. I've been having some issues with printing the result in TeXForm, though.

I have the following in my UltiSnips: (essentially same as the article)

priority 10
snippet math "mathematica block" i
math $1 math$0
endsnippet

priority 100
snippet 'math(.*)math' "evaluate mathematica" wr
!p
import subprocess
code = 'ToString[' + match.group(1) + ', TeXForm]'
snip.rv = subprocess.check_output(['wolframscript', '-code', code])

endsnippet


However, when I run an operation, I have some serious issues. All the backslashes \ are doubled (so \frac{1}{2} turns into \\frac{1}{2}), and the entire output is wrapped in b' and \n'

For example,

math 1+1 math


expands to

b'2\n'


and

math Roots[x^2-1/2 == 0, x] math


expands to

b'x=\\frac{1}{\\sqrt{2}}\\lor x=-\\frac{1}{\\sqrt{2}}\n'


I've fixed this temporarily by defining the b' - \n' as a text object, then recording an operation that deletes the text object and replaces all "\\" with "\". However, I really don't like this solution, and I feel like there's probably more issues that I haven't run into yet since I haven't used this setup for very long.

Any ideas?

• This sounds like a python bytes-string issue. Curious – D. Ben Knoble Jan 13 '20 at 16:29

I do not know whether you have found a solution in the mean time, but according to this post https://stackoverflow.com/questions/606191/convert-bytes-to-a-string you have to decode the bytes object, so that the snippet becomes:

snippet 'math(.*)math' "math" wr
!p
import subprocess
code = match.group(1)
code = 'ToString[' + code + ',TeXForm]'
res = subprocess.check_output(['wolframscript', '-code', code])
snip.rv=res.decode('utf-8')

endsnippet


by the way thank you for the very cool ideas you posted on https://castel.dev/post/lecture-notes-1/