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

Fix Latex support #95

Merged
merged 7 commits into from
Feb 14, 2016
Merged

Fix Latex support #95

merged 7 commits into from
Feb 14, 2016

Conversation

ccordoba12
Copy link
Collaborator

Fixes #56

This builds on top of ipython/ipython#9203, so that one has to be merged first.

Pinging @flying-sheep, @asmeurer and @jorisvandenbossche


Tasks:

  • Fix the case when neither Matplotlib nor Latex are installed. Qtconsole was displaying an empty image when it should move to use the plain text repr.
  • Fix the case when Matplotlib is incapable of handling Latex. Qtconsole was printing a huge traceback, even after pull request Fallback to the string printer if the latex printer raises an exception #65 .
  • Fix the case when Latex doesn't come in math mode. This will fix the issues with IRKernel and Pandas.
  • Bonus: First try to use Latex, then Matplotlib to generate the representation.

@ccordoba12 ccordoba12 added this to the 4.2 milestone Feb 2, 2016
@@ -140,9 +140,8 @@ def _handle_execute_result(self, msg):
self._pre_image_append(msg, prompt_number)
try:
self._append_latex(data['text/latex'], True)
except Exception:
self.log.error("Failed to render latex: '%s'", data['text/latex'], exc_info=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it a good thing to log failure to render the latex?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i think we should keep catching all exceptions and only in case of a ValueError (or probably a custom subclass we define) we swallow the error (as this one means that we got non-equation LaTeX)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it a good thing to log failure to render the latex?

Not for Spyder and Canopy because those failures are shown in the terminal where those applications were started. If they are started graphically, the errors won't be seen by their users.

Besides, (as I said in ipython/ipython#9203) possible errors are:

  1. Matplotlib is unable to handle Latex, something not easily discernible from the traceback.
  2. Latex has syntax errors, something that also generates one of those long and almost unreadable report errors.

So this simplifies things and handles all cases I mentioned at the beginning correctly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or probably a custom subclass we define

Yes, I was thinking to create a simple LatexError class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good points. it’s the latex-repr-writers’ responsibility to emit correct code anyway and if they don’t, they’ll have to debug it.

@asmeurer
Copy link
Contributor

asmeurer commented Feb 2, 2016

Regarding the last checkmark, it's worth noting that matplotlib's math renderer produces output that's considerably worse than latex. Example:

matplotlib

matplotlib_math

latex

latex_math

For SymPy we prefer latex and only fallback to matplotlib if it isn't installed.

@minrk
Copy link
Member

minrk commented Feb 2, 2016

@asmeurer thanks, following sympy's lead seems sensible here.

@ccordoba12
Copy link
Collaborator Author

Thanks @asmeurer for the input. I changed the task accordingly.

@flying-sheep
Copy link
Contributor

OK, then the only thing left is the other mathmode triggers (which only work for the dvipng backend)

inline:

  • \(...\)
  • $...$
  • environments (\begin{name}...\end{name}):
    • math

display:

  • \[...\]
  • $$...$$
  • environments (\begin{name}...\end{name}):
    • displaymath
    • equation/equation*
    • eqnarray/eqnarray*
    • multline/multline*
    • gather/gather*
    • align/align*
    • flalign/flalign*
    • alignat/alignat*

@ccordoba12
Copy link
Collaborator Author

@flying-sheep, I won't implement all that, for two reasons:

  1. Checking for dollar signs is enough for now, i.e. to have Sympy, IRKernel and Pandas work as expected.
  2. I don't have time for it.

Maybe you could do it after this PR is merged :-)

@ccordoba12
Copy link
Collaborator Author

Or you could send a PR against my branch if you want it as part of this.

@flying-sheep
Copy link
Contributor

no separately is OK

or let me do it inline

@ccordoba12
Copy link
Collaborator Author

Against my branch? Please be my guest :-)

In any case, thanks for helping me out.

@flying-sheep
Copy link
Contributor

OK. here it is. is this sufficient or should i do the PR?

basic_envs = 'math displaymath'.split()
starable_envs = 'equation eqnarray multline gather align flalign alignat'.split()
star_envs = [env + '*' for env in starable_envs]
envs = basic_envs + starable_envs + star_envs

env_syntax = [r'\begin{{{0}}} \end{{{0}}}'.format(env).split() for env in envs]

math_syntax = [
    (r'\[', r'\]'), (r'\(', r'\)'),
    ('$$', '$$'), ('$', '$'),
]

def is_latex_math(latex):
    for start, end in math_syntax + env_syntax:
        inner = latex[len(start):-len(end)]
        if start in inner or end in inner:
            return False
        if latex.startswith(start) and latex.endswith(end):
            return True
    return False

@ccordoba12
Copy link
Collaborator Author

I think it's enough, thanks a lot!

@flying-sheep
Copy link
Contributor

NP!

i edited it a bit since first commenting, so the version at the time of writing should be good

@ccordoba12
Copy link
Collaborator Author

@minrk, I think this one is ready. Please review it and let me know what you think.

minrk added a commit that referenced this pull request Feb 14, 2016
@minrk minrk merged commit 255076e into jupyter:master Feb 14, 2016
@ccordoba12 ccordoba12 deleted the fix-latex branch February 14, 2016 14:57
@flying-sheep
Copy link
Contributor

great, thanks!

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

Successfully merging this pull request may close these issues.

LaTeX support is broken
4 participants