Custom latex printing for Function subclass

53 views
Skip to first unread message

G B

unread,
Nov 21, 2014, 1:11:49 AM11/21/14
to sy...@googlegroups.com
I haven't moved to 0.7.6 yet, so I'm still using 0.7.5.

I'm trying to create a custom subclass of Function, which is sure to emit more questions later, but for now the question is: "how do I the LatexPrinter to display it as anything but name(x,y)"?  It looks like that's the behavior if there isn't a _print_name() function in LatexPrinter itself, and LatexPrinter doesn't appear easily extensible.

Is there a method I can add to my custom subclass that describes how it should be printed?


Aaron Meurer

unread,
Nov 21, 2014, 12:01:15 PM11/21/14
to sy...@googlegroups.com
Each printer has a method that you can define on your custom classes
that will define how to print your class with that printer. If you
search http://docs.sympy.org/latest/modules/printing.html for
"printmethod" you will find them. The one for the LaTeX printer is
called _latex. The docs should be improved, but the function signature
is like

def _latex(self, printer, *args)

printer will be the printer object itself. You should use it to
recursively print other objects. For example, if I had a class alpha
that I wanted to print like \alpha \left (x, y\right) I would do
something like

def _latex(self, printer, *args):
return r'\alpha \left (' + ', '.join(printer.doprint(arg for arg
in args) + r'\right )'

If you search the SymPy code for "def _latex" you'll find many examples.

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 post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/524cd627-5aa2-4002-85da-1d960555bf12%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages