# Use dvipng to format sympy.Matrix #1861

Merged
merged 7 commits into from Jun 11, 2012

None yet

### 3 participants

commented Jun 5, 2012
 Two changes are introduced: new dvipng backend is added to latex_to_png PNG formatter for sympy.Matrix uses the dvipng backend
added some commits Jun 5, 2012
 tkf Add dvipng backend to latex_to_png 196e1f3 tkf Use dvipng to format sympy.Matrix to PNG display 49a6541
Member
commented Jun 5, 2012
 This looks pretty nice. Though you mustn't use the dvipng backend unconditionally, as it may not be present.
commented Jun 5, 2012
 Quite right. Is returning None delegate the work to other formatter?
Member
commented Jun 5, 2012
 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 Jun 5, 2012
 I think latex output of Matrix can't rendered by matplotlib. So I guess returning None when dvipng is not present is OK here.
Member
commented Jun 5, 2012
 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 Jun 5, 2012
 Added comment/docstrings. 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.
Member
commented Jun 5, 2012
 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 Jun 5, 2012
 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
Member
commented Jun 5, 2012
 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.
Member
commented Jun 7, 2012
 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.
added some commits Jun 7, 2012
 tkf Suppress output from latex and dvipng command 25a5c3f tkf Check if latex/dvipng exist before calling them b1cc95f
commented Jun 7, 2012
 Done.
Member
commented Jun 10, 2012
 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).
Member
commented Jun 10, 2012
 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.
commented Jun 10, 2012
 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.
commented Jun 10, 2012
 Ah, notebook server will send unused data to notebook client, so I guess it will slow down the response a bit.
Member
commented Jun 11, 2012
 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.
Member
commented Jun 11, 2012
 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 Jun 11, 2012