Repository: sympy/sympy

Issue:
Maximum recursion depth exceeded in printer
expr = Pow(1,-1,evaluate=False)
latex(expr)


Discussion:

Patch:
diff --git a/sympy/printing/latex.py b/sympy/printing/latex.py
index 2c3e1979c6..a85a302ec3 100644
--- a/sympy/printing/latex.py
+++ b/sympy/printing/latex.py
@@ -514,6 +514,9 @@ def _print_Pow(self, expr):
                 return self._print(expr.base, exp="%s/%s" % (p, q))
             return r"%s^{%s/%s}" % (base, p, q)
         elif expr.exp.is_Rational and expr.exp.is_negative and expr.base.is_commutative:
+            # special case for 1^(-x), issue 9216
+            if expr.base == 1:
+                return r"%s^{%s}" % (expr.base, expr.exp)
             # things like 1/x
             return self._print_Mul(expr)
         else:


Test patch:
diff --git a/sympy/printing/tests/test_latex.py b/sympy/printing/tests/test_latex.py
index e5c8bfb5ad..f398c8f9f5 100644
--- a/sympy/printing/tests/test_latex.py
+++ b/sympy/printing/tests/test_latex.py
@@ -1776,3 +1776,17 @@ def test_issue_14041():
         r"\left(\dot{\phi} + \dot{\theta}\right)^{2}\mathbf{\hat{a}_x}"
     assert latex((phid*thetad)**a*A_frame.x) == \
         r"\left(\dot{\phi} \dot{\theta}\right)^{a}\mathbf{\hat{a}_x}"
+
+
+def test_issue_9216():
+    expr_1 = Pow(1, -1, evaluate=False)
+    assert latex(expr_1) == r"1^{-1}"
+
+    expr_2 = Pow(1, Pow(1, -1, evaluate=False), evaluate=False)
+    assert latex(expr_2) == r"1^{1^{-1}}"
+
+    expr_3 = Pow(3, -2, evaluate=False)
+    assert latex(expr_3) == r"\frac{1}{9}"
+
+    expr_4 = Pow(1, -2, evaluate=False)
+    assert latex(expr_4) == r"1^{-2}"