sympy printer for custom class

24 views
Skip to first unread message

brombo

unread,
Sep 9, 2022, 11:20:32 AM9/9/22
to sympy
I have written the custom class PieceWiseFunction.  One of the class members is:

    def _latex(self):
        s = r'\left \{\begin{array}{c}'+'\n'
        s += r'x < '+latex(self.x[0])+': '+latex(self.f[0])+r' \\ '+'\n'
        for i in range(1,len(self.x)):
            xlo = latex(self.x[i-1])
            xhi = latex(self.x[i])
            f   = latex(self.f[i])
            s += xlo+r' \le x < '+xhi+r': '+f+r' \\ '+'\n'
        s += r' x \ge '+latex(self.x[-1])+': '+latex(self.f[-1])+'\n'
        s += r' \end{array}\right \}'
        return s

I am trying to use the sympy "latex" function to access my _latex and get the following error message:

File "/home/brombo/.local/lib/python3.8/site-packages/sympy/printing/printer.py", line 309, in _print
    return getattr(expr, self.printmethod)(self, **kwargs)
TypeError: _latex() takes 1 positional argument but 2 were given

What am I doing wrong in my definition of _latex?

Aaron Meurer

unread,
Sep 10, 2022, 2:38:44 AM9/10/22
to sy...@googlegroups.com
The method should have the signature _latex(self, printer), and you
should use printer._print() to recursively print things. See
https://docs.sympy.org/latest/guides/custom-functions.html#printing

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/c0821032-a469-4506-afc9-880d0ea0d952n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages