Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

converting string to latex file

Hi, I'm in trouble fixing a little problem. My context: I do generate math expressions as strings. I'm able to use them as math expressions without any issue. However when I need to get the LaTeX format (to drop in a file for a later use) I'm obliged to use the eval function that kills the structure. here is the code my very naive code (as I'm just a neewby)

oper=[">", ">=", "<", "<="]
a=randint(-20,20)
b=randint(-10,10)
c,d=0,0
while c*x+d ==0:
    c=randint(-20,20)
    d=randint(-10,10)
f=(a*x+b)/(c*x+d)
a=randint(-20,20)
b=randint(-10,10)

    c,d=0,0
    while c*x+d ==0:
        c=randint(-20,20)
        d=randint(-10,10)
    g=(a*x+b)/(c*x+d)
    mystr= str(f) + str(sample(oper,1)[0]) + str(g)
    show(latex(mystr))
    print(latex(eval(mystr)))

I didn't want to set specific values for my variables and just dropped the whole piece of code to allow you guys to check if you got some time. Obviously for all those cases that doesn't have simplifications it works fine for me. You help will be much appreciated!

converting string to latex file

Hi, I'm in trouble fixing a little problem. My context: I do generate math expressions as strings. I'm able to use them as math expressions without any issue. However when I need to get the LaTeX format (to drop in a file for a later use) I'm obliged to use the eval function that kills the structure. here is the code my very naive code (as I'm just a neewby)

oper=[">", ">=", "<", "<="]
a=randint(-20,20)
b=randint(-10,10)
c,d=0,0
while c*x+d ==0:
    c=randint(-20,20)
    d=randint(-10,10)
f=(a*x+b)/(c*x+d)
a=randint(-20,20)
b=randint(-10,10)

    c,d=0,0
    while c*x+d ==0:
        c=randint(-20,20)
        d=randint(-10,10)
    g=(a*x+b)/(c*x+d)
    mystr= str(f) + str(sample(oper,1)[0]) + str(g)
    show(latex(mystr))
    print(latex(eval(mystr)))

I didn't want to set specific values for my variables and just dropped the whole piece of code to allow you guys to check if you got some time. Obviously for all those cases that doesn't have simplifications it works fine for me. You help will be much appreciated!