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

Ronan Lamy ronan.lamy@gmail....
Thu Nov 29 11:07:58 CST 2012

Le 17/11/2012 23: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.
> 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?

Here's my take on this, using str.format with a custom conversion flag: 

Usage looks like:
format_latex('$\\textrm{{With}}$ $\\tt sympy$, to get ${x!L}$, type 
$\\tt {x!s}$', x=pi**2/6)

More information about the IPython-User mailing list