Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Use dvipng to format sympy.Matrix #1861

Merged
merged 7 commits into from

3 participants

@tkf
tkf 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
@minrk
Owner

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

@tkf
tkf commented

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

@minrk
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 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

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

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

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

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

@minrk
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
tkf commented

Done.

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

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

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

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

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

    latex_to_png returns None when the backend cannot be used

    tkf authored
    When matplotlib/dvipng is not present, other formatter will be used.
  4. @tkf
  5. @tkf
Commits on Jun 7, 2012
  1. @tkf
  2. @tkf
This page is out of date. Refresh to see the latest.
Showing with 99 additions and 10 deletions.
  1. +20 −2 IPython/extensions/sympyprinting.py
  2. +79 −8 IPython/lib/latextools.py
View
22 IPython/extensions/sympyprinting.py
@@ -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)
View
87 IPython/lib/latextools.py
@@ -6,7 +6,7 @@
* Brian Granger
"""
#-----------------------------------------------------------------------------
-# Copyright (C) 2010-2011, IPython Development Team.
+# Copyright (C) 2010 IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
@@ -19,14 +19,20 @@
from StringIO import StringIO
from base64 import encodestring
+import os
+import tempfile
+import shutil
+import subprocess
+
+from IPython.utils.process import find_cmd, FindCmdError
#-----------------------------------------------------------------------------
# Tools
#-----------------------------------------------------------------------------
-def latex_to_png(s, encode=False):
- """Render a LaTeX string to PNG using matplotlib.mathtext.
+def latex_to_png(s, encode=False, backend='mpl'):
+ """Render a LaTeX string to PNG.
Parameters
----------
@@ -34,18 +40,82 @@ def latex_to_png(s, encode=False):
The raw string containing valid inline LaTeX.
encode : bool, optional
Should the PNG data bebase64 encoded to make it JSON'able.
+ backend : {mpl, dvipng}
+ Backend for producing PNG data.
+
+ None is returned when the backend cannot be used.
+
"""
- from matplotlib import mathtext
+ if backend == 'mpl':
+ f = latex_to_png_mpl
+ elif backend == 'dvipng':
+ f = latex_to_png_dvipng
+ else:
+ raise ValueError('No such backend {0}'.format(backend))
+ bin_data = f(s)
+ if encode and bin_data:
+ bin_data = encodestring(bin_data)
+ return bin_data
+
+
+def latex_to_png_mpl(s):
+ try:
+ from matplotlib import mathtext
+ except ImportError:
+ return None
mt = mathtext.MathTextParser('bitmap')
f = StringIO()
mt.to_png(f, s, fontsize=12)
- bin_data = f.getvalue()
- if encode:
- bin_data = encodestring(bin_data)
+ return f.getvalue()
+
+
+def latex_to_png_dvipng(s):
+ try:
+ find_cmd('latex')
+ find_cmd('dvipng')
+ except FindCmdError:
+ return None
+ try:
+ workdir = tempfile.mkdtemp()
+ tmpfile = os.path.join(workdir, "tmp.tex")
+ dvifile = os.path.join(workdir, "tmp.dvi")
+ outfile = os.path.join(workdir, "tmp.png")
+
+ with open(tmpfile, "w") as f:
+ f.write(_latex_header)
+ f.write(s)
+ f.write(_latex_footer)
+
+ subprocess.check_call(
+ ["latex", "-halt-on-errror", tmpfile], cwd=workdir,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+ subprocess.check_call(
+ ["dvipng", "-T", "tight", "-x", "1500", "-z", "9",
+ "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir,
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+ with open(outfile) as f:
+ bin_data = f.read()
+ finally:
+ shutil.rmtree(workdir)
return bin_data
+_latex_header = r'''
+\documentclass{article}
+\usepackage{amsmath}
+\usepackage{amsthm}
+\usepackage{amssymb}
+\usepackage{bm}
+\pagestyle{empty}
+\begin{document}
+'''
+
+_latex_footer = r'\end{document}'
+
+
_data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />"""
def latex_to_html(s, alt='image'):
@@ -59,7 +129,8 @@ def latex_to_html(s, alt='image'):
The alt text to use for the HTML.
"""
base64_data = latex_to_png(s, encode=True)
- return _data_uri_template_png % (base64_data, alt)
+ if base64_data:
+ return _data_uri_template_png % (base64_data, alt)
# From matplotlib, thanks to mdboom. Once this is in matplotlib releases, we
Something went wrong with that request. Please try again.