Saturday, 8 July 2023

display math in GoogleColab

Can someone explain me why the code below works in Jupyter (the string is updated and displayed when modified by the widget) but does not work in GoogleColab ? (And how to make it works in GoogleColab ?)

from IPython.display import display, Math

def combustion(nH2i,nO2i,nH2Oi):
    x = nH2i/2      
    if nO2i-x<0:    
        x = nO2i    

    nH2f = nH2i - 2*x
    nO2f =  nO2i - x
    nH2Of = nH2Oi + 2*x
    
    reponse = r"""$\begin{array}{cccc}
    2.H_2&+&O_2&\to&2.H_2O\\
    {nH2i}&+&{nO2i}&&{nH2Oi}\\
    {nH2f}&+&{nO2f}&&{nH2Of}\\
    \end{array}$"""
    liste = ["nH2i","nO2i","nH2Oi","nH2f","nO2f","nH2Of"]
    for x in liste:
        reponse = reponse.replace(x,str(eval(x)))
    display(Math(reponse))

from ipywidgets import interact
interact(combustion, nH2i=(0,10,1), nO2i=(0,10,1), nH2Oi=(0,10,1))

Edit: It doesn't work because it prints the uninterpreted LaTeX string (as shown in Mark comment) but it doesn't display it as a LaTeX interpeter should do: an array with the values !



from display math in GoogleColab

No comments:

Post a Comment