Permalink
Browse files

Add printing to LaTeX.

Also add the tests for printing to LaTeX.  Anonymous objects and
morphisms are supported.
  • Loading branch information...
1 parent 2e81231 commit 9334d84623cc3eec8a3ec6cc432980315f37de07 @scolobb committed Jun 4, 2012
Showing with 53 additions and 1 deletion.
  1. +27 −1 sympy/printing/latex.py
  2. +26 −0 sympy/printing/tests/test_latex.py
View
@@ -2,7 +2,7 @@
A Printer which converts an expression into its LaTeX equivalent.
"""
-from sympy.core import S, C, Add
+from sympy.core import S, C, Add, Symbol
from sympy.core.function import _coeff_isneg
from printer import Printer
from conventions import split_super_sub
@@ -1181,6 +1181,32 @@ def _print_DMP(self, p):
def _print_DMF(self, p):
return self._print_DMP(p)
+ def _print_Object(self, object):
+ if object.name:
+ return self._print(Symbol(object.name))
+ else:
+ return "\\bullet"
+
+ def _print_Morphism(self, morphism):
+ domain = self._print(morphism.domain)
+ codomain = self._print(morphism.codomain)
+ tail = "%s\\rightarrow %s" % (domain, codomain)
+
+ pretty_name = morphism.name
+ if pretty_name:
+ pretty_name = self._print(Symbol(pretty_name))
+ else:
+ for component in reversed(morphism.components):
+ if not component.name:
+ # Composition with an anonymous morphism is an
+ # anonymous morphism.
+ return tail
+
+ pretty_name += self._print(Symbol(component.name)) + "\\circ "
+
+ pretty_name = pretty_name[:-6]
+
+ return "%s:%s" % (pretty_name, tail)
def latex(expr, **settings):
r"""
@@ -569,3 +569,29 @@ def test_PolynomialRing():
assert latex(QQ[x, y]) == r"\mathbb{Q}\left[x, y\right]"
assert latex(QQ.poly_ring(x, y, order="ilex")) == \
r"S_<^{-1}\mathbb{Q}\left[x, y\right]"
+
+def test_categories():
+ from sympy.categories import Object, Morphism
+ A1 = Object("A1")
+ A2 = Object("A2")
+ A3 = Object("A3")
+
+ f1 = Morphism(A1, A2, "f1")
+ f2 = Morphism(A2, A3, "f2")
+ anonymous = Morphism(A1, A3, "")
+
+ assert latex(A1) == "A_{1}"
+ assert latex(f1) == "f_{1}:A_{1}\\rightarrow A_{2}"
+ assert latex(anonymous) == "A_{1}\\rightarrow A_{3}"
+ assert latex(f2*f1) == "f_{2}\\circ f_{1}:A_{1}\\rightarrow A_{3}"
+
+ assert latex(Object("")) == "\\bullet"
+
+ h = Morphism(A2, A3, "").compose(Morphism(A1, A2, ""), "h")
+ assert latex(h) == "h:A_{1}\\rightarrow A_{3}"
+
+ h = Morphism(A2, A3, "") * Morphism(A1, A2, "")
+ assert latex(h) == "A_{1}\\rightarrow A_{3}"
+
+ h = Morphism(A2, A3, "f") * Morphism(A1, A2, "")
+ assert latex(h) == "A_{1}\\rightarrow A_{3}"

0 comments on commit 9334d84

Please sign in to comment.