Use dvipng to format sympy.Matrix #1861

Merged
merged 7 commits into from Jun 11, 2012

3 participants

@tkf
tkf commented Jun 5, 2012

Two changes are introduced:

  1. new dvipng backend is added to latex_to_png
  2. PNG formatter for sympy.Matrix uses the dvipng backend
@minrk
IPython member

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

@tkf
tkf commented Jun 5, 2012

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

@minrk
IPython member

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 tkf latex_to_png returns None when the backend cannot be used
When matplotlib/dvipng is not present, other formatter will be used.
170a5ca
@tkf
tkf 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.

@minrk
IPython member

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
tkf 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.

@minrk
IPython member

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.

@tkf
tkf 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.

@minrk
IPython member

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.

@minrk
IPython member

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
tkf commented Jun 7, 2012

Done.

@fperez
IPython member

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).

@minrk
IPython member

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.

@tkf

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.

@tkf

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

@minrk
IPython member

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.

@fperez
IPython member

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.

@fperez fperez merged commit 7cd55e5 into ipython:master Jun 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment