public
Created

Strings with embedded LaTeX in IPython

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
from string import Formatter
from sympy import latex
class LatexFormatter(Formatter):
def convert_field(self, value, conversion):
if conversion == 'L':
try:
return value._repr_latex()
except AttributeError:
return latex(value)
else:
return super(LatexFormatter, self).convert_field(value, conversion)
def vformat(self, *args):
latex_string = super(LatexFormatter, self).vformat(*args)
return Latex(latex_string)
format_latex = LatexFormatter().format
# Example
from sympy import pi
format_latex('With $\\tt sympy$, to get ${x!L}$, type $\\tt {x!s}$', x=pi**2/6)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.