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

Add MathJax / Latex support for offline plot/iplot/FigureWidget #1243

Merged
merged 8 commits into from
Oct 30, 2018

Conversation

jonmmease
Copy link
Contributor

Overview

This PR adds support for displaying figures with LaTeX typesetting using:

  • plotly.offline.plot for standalone HTML
  • plotly.offline.iplot inline in the classic notebook
  • FigureWidget:
    • Classic notebook
    • JupyterLab

To be effective, these changes require unreleased changes in plotly.js master. In particular (plotly/plotly.js#2994). These will be available as plotly.js 1.42 which will be included with plotly.py 3.4.0 release.

plotly.offline.plot changes

A new include_mathjax argument has been added to plotly.offline.plot. This argument mirrors the include_plotlyjs argument and supports a subset of its options. It defaults to False, matching the existing behavior of not adding any MathJax related code to the resulting HTML file/div. Setting it to 'cdn' adds a script tag referencing a predefined CDN locaiton, and setting it to a string ending with '.js' allows the user to specify a custom CDN path to be included in the script tag.

Full docstring

    include_mathjax (False | 'cdn' | path - default=False) --
        Specifies how the MathJax.js library is included in the output html
        file or div string.  MathJax is required in order to display labels
        with LaTeX typesetting.

        If False, no script tag referencing MathJax.js will be included in the
        output. HTML files generated with this option will not be able to
        display LaTeX typesetting.

        If 'cdn', a script tag that references a MathJax CDN location will be
        included in the output.  HTML files generated with this option will be
        able to display LaTeX typesetting as long as they have internet access.

        If a string that ends in '.js', a script tag is included that
        references the specified path. This approach can be used to point the
        resulting HTML file to an alternative CDN.

iplot and FigureWidget

The iplot and FigureWidget changes do not involve any API updates. They make it possible for plotly.js to take advantage of the MathJax instance already in use by the Jupyter Notebook and JupyterLab.

Outstanding issues

The only outstanding issue is that unlike the classic notebook, JupyterLab doesn't seem to load the global MathJax instance until it is needed by the markdown renderer. See jupyter-widgets/ipywidgets#2253. This may not be something we can address in the 3.4.0 timeframe.

@jonmmease jonmmease added this to the v3.4.0 milestone Oct 24, 2018
we don't break existing code that relied on positional indexes
# Conflicts:
#	plotly/offline/offline.py
#	plotly/tests/test_core/test_offline/test_offline.py
'/ajax/libs/mathjax/2.7.5/MathJax.js')))
elif (isinstance(include_mathjax, six.string_types) and
include_mathjax.endswith('.js')):
mathjax_script = '<script src="{url}"></script>'.format(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needs ?config=TeX-AMS-MML_SVG as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, add STIX_Web font config from mathjax_config

Received value: {val}

include_mathjax may be specified as False, 'cdn', or a string ending with '.js'
""".format(typ=type(include_mathjax), val=include_mathjax))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

use val=repr(include_mathjax) to be a bit more clear


if include_mathjax == 'cdn':
mathjax_script = (
'<script src="{url}?config=TeX-AMS-MML_SVG"></script>'.format(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add STIX_Web font config from mathjax_config. This way appearance of exported html will match iplot and FigureWidget

Choose a reason for hiding this comment

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

Is there an example on how to use include_mathjax='cdn' in a Jupyter notebook? For instance where would I enable LaTeX in plotly in the following block?

import plotly.offline as py
py.init_notebook_mode()
import plotly.graph_objs as go
from ipywidgets import interact
fig = go.FigureWidget()

I don't seem to get LaTeX working in the resulting interactive plots.
Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @dealmeidavf ,

include_mathajx is only needed for plotly.offline.plot. For FigureWidget and plotly.offline.iplot Jupyter's internal MathJax should be used automatically.

import plotly.graph_objs as go
go.FigureWidget(layout={'title': '$\LaTeX$'})

newplot 16

Could you open a new issue with your browser info, and whether you're using the classic notebook of JupyterLab? There are some issues with FireFox still, so try it out with Chrome if you haven't. Also, in JupyterLab you sometimes need to run a markdown cell containing LaTeX first so that the MathJax is initialized.

Choose a reason for hiding this comment

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

Thank you. I am using Jupyter notebook in the anaconda navigator on a Mac. LaTeX does not work on a Mac Firefox. It works on Safari. I am new to plotly but a user of matplotlib. The interactive plots in plotly using ipywidgets are amazing!

@jonmmease jonmmease merged commit 9875f66 into master Oct 30, 2018
michaelbabyn pushed a commit to michaelbabyn/plotly.py that referenced this pull request Dec 22, 2018
…ly#1243)

* Add include_mathjax argument to plotly.offline.plot
* Add MathJax support for plotly.offline.iplot in the notebook
* Add MathJax/LaTeX support for FigureWidget
@nicolaskruchten nicolaskruchten deleted the mathjax_support branch June 19, 2020 16:10
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.

None yet

2 participants