'Output LaTeX on matplotlib as text from user input. Backslashes cannot be parsed

I am trying to make a function that takes LaTeX input and shows it on a matplotlib as text. The function works perfectly fine if I pass the LaTeX directly into the plt.text, but when I try to pass in the input string, it seems like the backslashes in the LaTeX change their behaviour.

This is my code along with all the attempts I was able to find:

import matplotlib.pyplot as plt                                                 
from sympy import *   
def latex_out():

    # It's also possible to use the reduced notation by directly setting font.family:
    plt.rcParams.update({
    "text.usetex": True,
    "font.family": "Helvetica"
    })

    la= '$\int \sqrt{\frac{1}{x}}\, dx$'
    lat  = r'$\int \sqrt{\frac{1}{x}}\, dx$'
    lat2 = r'%s' % la
    lat3 = r"{}".format(la)
    lat4 = la.encode('unicode_escape')
    lat5 = la.encode('unicode-escape').decode().replace('\\\\', '\\')
    lat6 = r'{0}'.format(la)
    lat7 = fr"{la}"
    lat8 = repr(la)[1:-1]

    #add text                                                                       
    plt.text(.5, 0.6, r"%s" % la, fontsize = 30,ha='center', va='center')                                  

    #hide axes                                                                      
    fig = plt.gca()                                                                 
    fig.axes.get_xaxis().set_visible(False)                                         
    fig.axes.get_yaxis().set_visible(False)                                         
    plt.draw() #or savefig                                                          
    plt.show()


latex_out()

The errors I get show that the backslash for example in \int is read as \\int.

I found this code online that works, but I unsure why this one works and my code doesn't

import matplotlib.pyplot as plt                                                 
from sympy import *                                                                 


def latex_show():
    x = symbols('x')                                                          
    y = Integral(sqrt(1/x),x)
    lat = latex(y) 
    
    #add text                                                                       
    plt.text(.5, 0.6, r"$%s$" % lat, fontsize = 30,ha='center', va='center')                                  

    #hide axes                                                                      
    fig = plt.gca()                                                                 
    fig.axes.get_xaxis().set_visible(False)                                         
    fig.axes.get_yaxis().set_visible(False)                                         
    plt.draw() #or savefig                                                          
    plt.show()

Edit: I was able to get the function to work if my input has double back slashes, for example: '$\\int \\sqrt{\\frac{1}{x}}\\, dx$' instead of '$\int \sqrt{\frac{1}{x}}\, dx$'

Is there a way around this with user entering one backslash?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source