Created
November 29, 2012 16:50
-
-
Save rlamy/4170308 to your computer and use it in GitHub Desktop.
Strings with embedded LaTeX in IPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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