latex notebook printing of lists of custom classes

29 views
Skip to first unread message

Alan Bromborsky

unread,
Aug 26, 2014, 6:57:31 PM8/26/14
to sympy
I am trying to print a list of instances of a custom class. The input is -

print (o3d.grad, o3d.grad)

the output is (custom latex printer)

$$ (e_{x}
      \frac{\partial}{\partial x} + e_{y} \frac{\partial}{\partial y} +
      e_{z} \frac{\partial}{\partial z}, e_{x} \frac{\partial}{\partial
      x} + e_{y} \frac{\partial}{\partial y} + e_{z}
      \frac{\partial}{\partial z}) $$

however  in ipyrhon notebook I input

(o3d.grad, o3d.grad)

I get out
(e_{x} \frac{\partial}{\partial x} + e_{y} \frac{\partial}{\partial y} + e_{z} \frac{\partial}{\partial z},
 e_{x} \frac{\partial}{\partial x} + e_{y} \frac{\partial}{\partial y} + e_{z} \frac{\partial}{\partial z})
Also the standard latex tuple and list printer is not using \left ( \right  and \left [ \right ) to enclose
the tuple or list.  How can I write a custom latex tuple and list printer that will also work in ipython notebook.

Jason Moore

unread,
Aug 26, 2014, 7:38:41 PM8/26/14
to sy...@googlegroups.com
Notice how some odd classes that don't fit the sympy standards have special cases. Try adding your class to that list and see if it works.

--
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/53FD10D1.6080309%40verizon.net.
For more options, visit https://groups.google.com/d/optout.

Aaron Meurer

unread,
Aug 26, 2014, 7:46:33 PM8/26/14
to sy...@googlegroups.com
On Tue, Aug 26, 2014 at 5:57 PM, Alan Bromborsky <abr...@verizon.net> wrote:
I am trying to print a list of instances of a custom class. The input is -

print (o3d.grad, o3d.grad)

the output is (custom latex printer)

$$ (e_{x}
      \frac{\partial}{\partial x} + e_{y} \frac{\partial}{\partial y} +
      e_{z} \frac{\partial}{\partial z}, e_{x} \frac{\partial}{\partial
      x} + e_{y} \frac{\partial}{\partial y} + e_{z}
      \frac{\partial}{\partial z}) $$

however  in ipyrhon notebook I input

(o3d.grad, o3d.grad)

I get out
(e_{x} \frac{\partial}{\partial x} + e_{y} \frac{\partial}{\partial y} + e_{z} \frac{\partial}{\partial z},
 e_{x} \frac{\partial}{\partial x} + e_{y} \frac{\partial}{\partial y} + e_{z} \frac{\partial}{\partial z})
Also the standard latex tuple and list printer is not using \left ( \right  and \left [ \right ) to enclose

It should be, using the latest git master of SymPy. It was using matrices, but I change this recently because there were compatibility issues with matrices and matplotlib, so lists and tuples should just print with \left [ \right ] and \left ( \right ) now.

Aaron Meurer

the tuple or list.  How can I write a custom latex tuple and list printer that will also work in ipython notebook.

--

Alan Bromborsky

unread,
Aug 26, 2014, 8:07:18 PM8/26/14
to sy...@googlegroups.com
Can you suggest how I could override the function without modifying interactive/printing.py?

Aaron Meurer

unread,
Aug 26, 2014, 8:09:54 PM8/26/14
to sy...@googlegroups.com
You can define methods on the class, depending on what printer you want to override. For the LaTeX printer, it's _latex. The signature should be _latex(self, printer), and it should use printer to recursively print any subexpressions. See http://docs.sympy.org/latest/modules/printing.html#module-sympy.printing.latex (the printmethod attribute), and search the SymPy codebase for some examples. 

Aaron Meurer


Alan Bromborsky

unread,
Aug 26, 2014, 8:20:38 PM8/26/14
to sy...@googlegroups.com
Are you saying if my class is Dop I should have

class Dop(object):

    def _latex(self,GaLatexPrinter):
        etc.

where GaLatexPrinter is my custom latex printer that has member _print_Dop.

Jason Moore

unread,
Aug 26, 2014, 8:22:18 PM8/26/14
to sy...@googlegroups.com
Yes, one way to have latex printing is to add  _latex method. Then maybe it would be worth add this line to _can_print_latex:

https://github.com/sympy/sympy/blob/master/sympy/interactive/printing.py#L105

Then anything with the _latex printing method will print.

Alan Bromborsky

unread,
Aug 27, 2014, 9:10:49 AM8/27/14
to sy...@googlegroups.com
I looked at the TableForm class in sympy/printing/tabelform.py and have several questions relating to _latex(self,printer).

1.  In order to handle text and latex printing in a unified manner is _sympystr(self, p) the text equivalent of _latex(self,printer).

2. I wish to have both a latex and string printer for my Dop (differential operator) class.  Currently I have custom printers GaPrinter for
text and GaLatexPrinter for latex.  Members of my Dop class is Dop_latex_str(self) which generates the latex string for a Dop and
Dop_str(self) which generates the text string for a Dop.  In the GaLatexPrinter class I have the member

    def _print_Dop(self, expr):
        return expr.Dop_latex_str()

and in the GaPrinter class the member

    def _print_Dop(self, expr):
        return expr.Dop_str()

Should I redefine Dop_latex_str(self) as _latex(self,printer) and Dop_str(self) as _sympystr(self, printer)?

3.  If I define _latex(self,printer) and _sympystr(self, printer) do I need _print_Dop(self, expr) in the GaLatexPrinter and GaPrinter classes?

4. Are the only members I need in the custom printer classes the ones that are overriding the ones in the default printer classes such as
_print_Derivative(self, expr) and _print_Function(self, expr)?

Aaron Meurer

unread,
Sep 9, 2014, 7:56:30 PM9/9/14
to sy...@googlegroups.com
On Wed, Aug 27, 2014 at 8:10 AM, Alan Bromborsky <abr...@verizon.net> wrote:
I looked at the TableForm class in sympy/printing/tabelform.py and have several questions relating to _latex(self,printer).

1.  In order to handle text and latex printing in a unified manner is _sympystr(self, p) the text equivalent of _latex(self,printer)  
.

 

2. I wish to have both a latex and string printer for my Dop (differential operator) class.  Currently I have custom printers GaPrinter for
text and GaLatexPrinter for latex.  Members of my Dop class is Dop_latex_str(self) which generates the latex string for a Dop and
Dop_str(self) which generates the text string for a Dop.  In the GaLatexPrinter class I have the member

    def _print_Dop(self, expr):
        return expr.Dop_latex_str()

and in the GaPrinter class the member

    def _print_Dop(self, expr):
        return expr.Dop_str()

Should I redefine Dop_latex_str(self) as _latex(self,printer) and Dop_str(self) as _sympystr(self, printer)?

There are two ways to do it. You have a custom printer, and define methods on the class, or you define special methods on your class itself. There are advantages and disadvantages to each way, depending on how you modularize your code. There's no need to do both, though. 

For example, if you want to change the way a class that you don't control prints, you should create a custom printer class. If you want to customize the way your own object prints with an existing printer without editing that printer, you should use the method on your class.
 

3.  If I define _latex(self,printer) and _sympystr(self, printer) do I need _print_Dop(self, expr) in the GaLatexPrinter and GaPrinter classes?

No. In fact, the methods on the classes will take precedence over the methods on the printer (http://docs.sympy.org/latest/modules/printing.html#module-sympy.printing.printer).
 

4. Are the only members I need in the custom printer classes the ones that are overriding the ones in the default printer classes such as
_print_Derivative(self, expr) and _print_Function(self, expr)?

If you don't want to change a method you don't need to override it. 

Aaron Meurer
 
Reply all
Reply to author
Forward
0 new messages