Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Strings with embedded LaTeX in IPython
from string import Formatter
from sympy import latex
class LatexFormatter(Formatter):
def convert_field(self, value, conversion):
if conversion == 'L':
return value._repr_latex()
except AttributeError:
return latex(value)
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment