Skip to content

Instantly share code, notes, and snippets.

@rlamy
Created November 29, 2012 16:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rlamy/4170308 to your computer and use it in GitHub Desktop.
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':
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment