From a242dd35cc1b077fb2defbca83e59f7178355ac7 Mon Sep 17 00:00:00 2001 From: Stuart Mumford Date: Fri, 29 May 2015 20:53:13 +0100 Subject: [PATCH] Add a repr_latex_formatter Add a new default formatter that checks for the existence of a `_repr_latex_` method before calling `str` --- pythontex/pythontex_utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pythontex/pythontex_utils.py b/pythontex/pythontex_utils.py index 2731e7a..44d515d 100644 --- a/pythontex/pythontex_utils.py +++ b/pythontex/pythontex_utils.py @@ -355,6 +355,15 @@ def _sympy_latex(expr, **settings): return r'\mathchoice{' + display + '}{' + text + '}{' + script + '}{' + scriptscript+ '}' self._sympy_latex = _sympy_latex + def repr_latex_formatter(self, expr): + if hasattr(expr, '_repr_latex_'): + return expr._repr_latex_() + else: + if sys.version_info[0] == 2: + return unicode(expr) + else: + return str(expr) + # Now we are ready to create non-SymPy formatters and a method for # setting formatters def identity_formatter(self, expr): @@ -364,7 +373,7 @@ def identity_formatter(self, expr): ''' return expr - def set_formatter(self, fmtr='str'): + def set_formatter(self, fmtr='repr_latex'): ''' Set the formatter method. @@ -380,6 +389,8 @@ def set_formatter(self, fmtr='str'): self.formatter = self.sympy_latex elif fmtr in ('None', 'none', 'identity') or fmtr is None: self.formatter = self.identity_formatter + elif fmtr == 'repr_latex': + self.formatter = self.repr_latex_formatter else: raise ValueError('Unsupported formatter type')