Skip to content

Work out how to integrate GaLatexPrinter._print_Function upstream #276

@eric-wieser

Description

@eric-wieser

Our copy of this function seems to have originated from sympy/sympy@c1fdd85

The diff below shows the changes made in galgebra

diff --git a/galgebra/printer.py b/galgebra/printer.py
index 3d3b515..e5bb2b0 100644
+-- a/galgebra/printer.py
-++ b/galgebra/printer.py
@@ -763,31 +763,30 @@ class GaLatexPrinter(LatexPrinter):
     def _print_Function(self, expr, exp=None):
 
         func = expr.func.__name__
+        name = func
-
         if hasattr(self, '_print_' + func):
             return getattr(self, '_print_' + func)(expr, exp)
         else:
+            args = [str(self._print(arg)) for arg in expr.args]
+
-            args = [ str(self._print(arg)) for arg in expr.args ]
             # How inverse trig functions should be displayed, formats are:
             # abbreviated: asin, full: arcsin, power: sin^-1
+            #inv_trig_style = self._settings['inv_trig_style']
+            _inv_trig_style = GaLatexPrinter.inv_trig_style
-            inv_trig_style = self._settings['inv_trig_style']
             # If we are dealing with a power-style inverse trig function
             inv_trig_power_case = False
             # If it is applicable to fold the argument brackets
             can_fold_brackets = self._settings['fold_func_brackets'] and \
+                len(args) == 1 and not self._needs_function_brackets(expr.args[0])
-                len(args) == 1 and \
-                not self._needs_function_brackets(expr.args[0])
 
+            inv_trig_table = ["asin", "acos", "atan", "acot", "acosh", "asinh", "atanh"]
-            inv_trig_table = ["asin", "acos", "atan", "acot"]
 
             # If the function is an inverse trig function, handle the style
             if func in inv_trig_table:
+                if GaLatexPrinter.inv_trig_style == "abbreviated":
-                if inv_trig_style == "abbreviated":
                     func = func
+                elif GaLatexPrinter.inv_trig_style == "full":
-                elif inv_trig_style == "full":
                     func = "arc" + func[1:]
+                elif GaLatexPrinter.inv_trig_style == "power":
-                elif inv_trig_style == "power":
                     func = func[1:]
                     inv_trig_power_case = True
 
@@ -804,21 +803,15 @@ class GaLatexPrinter(LatexPrinter):
                 if func in accepted_latex_functions:
                     name = r"\%s^{%s}" % (func, exp)
                 else:
+                    name = latex(Symbol(func)) + ' '
+                    if '_' in func or '^' in func:
+                        name = r'{\left ( ' + name + r'\right ) }^{' + exp + '}'
+                    else:
+                        name += '^{' + exp + '}'
-                    # If the generic function name contains an underscore, handle it
-                    name = r"\operatorname{%s}^{%s}" % (
-                        func.replace("_", r"\_"), exp)
             else:
                 if func in accepted_latex_functions:
                     name = r"\%s" % func
                 else:
+                    name = latex(Symbol(func)) + ' '
+                    if exp is not None:
+                        if '_' in name or '^' in name:
+                            name = r'\left ( ' + name + r'\right )^{' + exp + '}'
+                        else:
+                            name += '^{' + exp + '}'
-                    # If the generic function name contains an underscore, handle it
-                    name = r"\operatorname{%s}" % func.replace("_", r"\_")
 
             if can_fold_brackets:
                 if func in accepted_latex_functions:
@@ -826,25 +819,14 @@ class GaLatexPrinter(LatexPrinter):
                     # with the function name itself
                     name += r" {%s}"
                 else:
+                    if not GaLatexPrinter.Fmode:
+                        name += r"%s"
-                    name += r"%s"
             else:
+                if func in accepted_latex_functions or not GaLatexPrinter.Fmode:
+                    name += r"{\left (%s \right )}"
-                name += r"{\left (%s \right )}"
 
             if inv_trig_power_case and exp is not None:
                 name += r"^{%s}" % exp
 
+            if func in accepted_latex_functions or not GaLatexPrinter.Fmode:
+                if len(args) == 1:
+                    name = name % args[0]
+                else:
+                    name = name % ",".join(args)
+
+            if 'det(g)' in name:
+                name = name.replace('det(g)', r'\det\left ( g \right )')
+
+            return name
-            return name % ",".join(args)
 
     def _print_Derivative(self, expr):
         dim = len(expr.variables)

We should work out ways to either integrate these upstream, or achieve them without modifying this function.

Listing the changes explicitly:

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: printerstring, latex, and ansi escape printing

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions