# ipython/ipython

### Subversion checkout URL

You can clone with
or
.

# Use dvipng to format sympy.Matrix#1861

Merged
merged 7 commits into from
+99 −10

### 3 participants

commented

Two changes are introduced:

1. new dvipng backend is added to latex_to_png
2. PNG formatter for sympy.Matrix uses the dvipng backend
 tkf Add dvipng backend to latex_to_png 196e1f3 tkf Use dvipng to format sympy.Matrix to PNG display 49a6541
Owner

This looks pretty nice. Though you mustn't use the dvipng backend unconditionally, as it may not be present.

commented

Quite right. Is returning None delegate the work to other formatter?

Owner

returning None will delegate to other mimetypes, but if you want to fallback on the mpl backend, then you have to do that yourself.

 tkf latex_to_png returns None when the backend cannot be used When matplotlib/dvipng is not present, other formatter will be used. 170a5ca
commented

I think latex output of Matrix can't rendered by matplotlib. So I guess returning None when dvipng is not present is OK here.

Owner

That makes sense, can you add a comment to that effect? The function itself looks generic, so it's unclear why it would never be used with mpl.

And please don't do pep8 cleanup on code other than your own in the future.

 tkf Document print_png and print_display_png 3d0539d
commented

Sorry about PEP8. I just removed it. I remember the discussion (I think it was about copyright year stuff) saying this kind of cleanup should be done when the code is touched. Now I guess it was only for core developers.

Owner

Thanks!

IPython doesn't enforce pep8, so not all changes it would make should be made just for the sake of it. The copyright discussion specifically referred to the copyright notice at the top of the file, which everyone is welcome to fix as they edit fies. Even (or especially) core devs don't do file-wide pep8 at any time.

commented

I had impression that IPython devs try to follow pep8 as much as possible because devs try to apply pep8 even to JS. But anyway, lesson learned! Thanks for the explanation.

 tkf Copyright fix on sympyprinting.py and latextools.py See: #1644 483c5d8
Owner

We have been more strict in the JS because it's unfamiliar territory for all of us, so it helps us all stay on the same page while learning a new language.

We are much more comfortable in Python, and thus more comfortable letting authors make personal style choices in Python code. pep8 is a great guideline, and "when in doubt: pep8", but that doesn't mean an 82 character line needs to be wrapped.

Owner

I don't have dvipng on my machine, so I'm just checking that it does the right thing. It appears to do the right thing at the Python level, but latex barfs a bunch of crap to the terminal. Can you squash that by capturing stdout/err on your subprocesses?

You might also want to use IPython.utils.process.find_cmd on dvipng before even trying, because then you can be fairly sure it's not worth it.

 tkf Suppress output from latex and dvipng command 25a5c3f tkf Check if latex/dvipng exist before calling them b1cc95f
commented

Done.

Owner

Sorry if it's a bit of a dumb question: why is this nicer than using mathjax? In general, mathjax renders more cleanly in the browser, zooms correctly, etc. Dvipng solutions tend to suffer from nasty resolution-dependent behavior (which are likely to get worse with systems like the upcoming macs with retina displays).

I'm just trying to understand and make sure this is really always an improvement (since it's activated unconditionally).

Owner

This is for the QtConsole, and not relevant to the notebook. The notebook will still display the math with mathjax, ignoring the png this produces.

I think it is an improvement because mathjax does not work in Qt console. My intention to add dvipng backend to latex_to_png function is to cover latex math expressions which are not covered by matplotlib (such as the one by sympy.Matrix). Besides, if you put latex mime-type notebook won't show png (Am I understanding it correctly?). So I think it is improvement (or at least zero improvement) for all frontends.

Ah, notebook server will send unused data to notebook client, so I guess it will slow down the response a bit.

Owner

But this is already true of all the other sympy latex, so no big deal to add Matrices to the list.

The long-term answer is to render latex client-side in the QtConsole, which I think is preferable for various reasons.

Owner

Ah, got it. Then it's good to go, I just wanted to clarify that. I'll explain in the merge commit.

@tkf, thanks for the contribution! Merging now, thanks Min for the careful review.

merged commit 7cd55e5 into ipython:master
Commits on Jun 5, 2012
1. tkf committed
2. tkf committed
3. tkf committed
When matplotlib/dvipng is not present, other formatter will be used.
4. tkf committed
5. tkf committed
See: #1644
Commits on Jun 7, 2012
1. tkf committed
2. tkf committed
 @@ -11,7 +11,7 @@ """ #----------------------------------------------------------------------------- -# Copyright (C) 2008-2011 The IPython Development Team +# Copyright (C) 2008 The IPython Development Team # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. @@ -45,7 +45,9 @@ def print_basic_unicode(o, p, cycle): def print_png(o): - """A function to display sympy expression using LaTex -> PNG.""" + """ + A function to display sympy expression using inline style LaTeX in PNG. + """ s = latex(o, mode='inline') # mathtext does not understand certain latex flags, so we try to replace # them with suitable subs. @@ -54,6 +56,19 @@ def print_png(o): png = latex_to_png(s) return png + +def print_display_png(o): + """ + A function to display sympy expression using display style LaTeX in PNG. + """ + s = latex(o, mode='plain') + s = s.strip('\$') + # As matplotlib does not support display style, dvipng backend is + # used here. + png = latex_to_png('$$%s$$' % s, backend='dvipng') + return png + + def can_print_latex(o): """ Return True if type o can be printed with LaTeX. @@ -115,6 +130,9 @@ def load_ipython_extension(ip): png_formatter.for_type_by_name( 'sympy.core.basic', 'Basic', print_png ) + png_formatter.for_type_by_name( + 'sympy.matrices.matrices', 'Matrix', print_display_png + ) for cls in [dict, int, long, float] + printable_containers: png_formatter.for_type(cls, print_png)