Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeX that matplotlib fails on should fallback to the next renderer #63

Closed
asmeurer opened this issue Oct 19, 2015 · 4 comments · Fixed by #65
Closed

LaTeX that matplotlib fails on should fallback to the next renderer #63

asmeurer opened this issue Oct 19, 2015 · 4 comments · Fixed by #65
Assignees
Labels
Milestone

Comments

@asmeurer
Copy link
Contributor

There is a lot of advanced LaTeX that the matplotlib renderer cannot handle. If an object defines a LaTeX repr that it can't handle, it just shows the error message. It should fallback to the next renderer.

This is causing an issue for us with SymPy (see sympy/sympy#9962). SymPy is able to print expressions using Unicode, but when the renderer fails (in this case, for matrices), it just shows the error

Failed to render latex: 
\left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]
      ^
Expected "\right" (at char 6), (line:1, col:7)

To reproduce:

  • First, make sure you don't have latex installed in your PATH (or else it will just use that and not matplotlib)
  • Install the latest version of SymPy
  • Start the qtconsole and run
from sympy import *
init_session()
Matrix([[1, 2], [3, 4]])
@asmeurer
Copy link
Contributor Author

I am willing to send a fix for this if someone can indicate what the correct fix should be. My thinking is:

  • Make the latex printer special in that it falls back if the formatter fails.
  • Don't print the warning in the console (is there some kind of debug log I can print it to instead?)

@ccordoba12 ccordoba12 self-assigned this Oct 19, 2015
@ccordoba12 ccordoba12 added the bug label Oct 19, 2015
@ccordoba12 ccordoba12 added this to the 4.2 milestone Oct 19, 2015
@ccordoba12
Copy link
Collaborator

Thanks @asmeurer. This is also related to issue #56 and PR #57.

I'll look into it for 4.2.

@asmeurer
Copy link
Contributor Author

I didn't see those. Thanks for the reference.

I think this boils down to the fact that there is no way for frontends to change the printer precedence (in the qtconsole or the notebook). This is something that SymPy would like to have as well (we basically work around it by having options to init_printing that cause various printers to not be enabled at all). That's a much bigger issue to fix, though.

That PR looks extreme, and would probably break things for people. For SymPy, it would actually work, because our printing isn't enabled without calling init_printing() anyway, and we define our own png repr using latex/dvipng or matplotlib (and our printers do the right thing in this case, falling back to the text printer if matplotlib can't handle the LaTeX).

I'm not clear if the fix I'm suggesting would fix @flying-sheep's issue, unless it is also coming from LaTeX that matplotlib cannot render.

@asmeurer asmeurer changed the title LaTeX that matplotlib should fallback to the next renderer LaTeX that matplotlib fails on should fallback to the next renderer Oct 20, 2015
asmeurer added a commit to asmeurer/qtconsole that referenced this issue Oct 23, 2015
Currently it just prints an error and gives an empty png. However, there is a
lot of LaTeX that matplotlib cannot parse, e.g., matrices. This gives a better
user experience for SymPy, which has a text printer that it can fallback
to. SymPy cannot just disable the LaTeX printer because it is desired for the
notebook, and there is no way for it to enable LaTeX just in the notebook, or
change the printer precedence order.

I have left in the debug logging of the error (to the terminal).

Fixes jupyter#63. This supersedes jupyter#57, although I don't know if it fixes the issue
there.
@asmeurer
Copy link
Contributor Author

Here is a fix #65. It's maybe not the cleanest thing, but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants