[IPython-User] Printing a string and latex together in a notebook

Matthias BUSSONNIER bussonniermatthias@gmail....
Sun Nov 18 04:46:28 CST 2012

Le 18 nov. 2012 à 00:40, Mike Witt a écrit :

> In an ipython notebook, I'd like to do something like this:
>   x = Symbol('x')
>   print('The answer is: $%s$' %latex(x**2))
> The closest I've figured out how to get is:
>   print('The answer is:')
>   display(x**2)
> Or something equivalent to that. I'd like to mix the string
> and the math output on the same line, but I can't figure
> out the syntax.

In the general case, I don't think it is super simple.
You will have to get the function that compute the repr_tex of x**2 and use

from IPython.display import Latex
Latex('your formatted latex string here')

In your case, sympy provide an easy way to get latex repr when possible.
So in your case this should work (watch out for sympy.latex with lowercase L an IPython.Latex  with Upper Case L)

from sympy import Symbol, latex
from IPython.display import Latex
x= Symbol('x')
Latex(' I can have inline latex : $%s$'% latex(x**2))

> Note that I'm in a code cell, not a markdown cell. I want to
> calculate the answer and then print it with informative strings
> intermixed with the math. Is this possible?

This is not yet alway possible or easy, but we are working on way to improve that.

> -Mike
> _______________________________________________
> IPython-User mailing list
> IPython-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user

More information about the IPython-User mailing list