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

init_printing/init_session shouldn't generate png math by default #24983

Open
asmeurer opened this issue Mar 28, 2023 · 7 comments · May be fixed by #25374
Open

init_printing/init_session shouldn't generate png math by default #24983

asmeurer opened this issue Mar 28, 2023 · 7 comments · May be fixed by #25374

Comments

@asmeurer
Copy link
Member

If you call init_printing or init_session in a Jupyter notebook, by default it generates both mathjax and png output. For example, this notebook

Screen Shot 2023-03-27 at 7 15 21 PM

generates this JSON:

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a724c220",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "init_printing()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4d700e39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA0AAAArCAYAAACjHgJXAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABPUlEQVRIDe2VMW7CQBBF7cABouQGHAFIbymhS5srwA2gtFtSpiRlKpLcAAqfICWlWzpER2neXy3WetdWonQgRvramdn5u57xaDYuyzJqkizL+vi/wAB978Z0XYPNW+x3sAND0AOB+CSd+KIoDpiy6LZAbgLPHxxXki3SGRfi3qZw5//v2G9Y2kdNKnkC6sUfUIAVewvWKCDJ+ZuccfVaU7vEnOI0TZtnWGsZrh1RleYSO+JfOVXvE0NjTnnGQMOkTRbETcxgsQQFroBID2AJ9BrKXoOIOE2mqAP02h1wvCVJUuR5/oz9gb1Bf0RfK5i9LbqRrmWbE6xvhO/V6rrRzDprm6VWCIJ1694J6ONz7ZCEZwKU10l6J8Vdq5s4UQmrep9uAH6N55pUJLwiFA2fM6oxMFySNmdewLdnG/MIWJ5aYR6r8pQAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\frac{1}{\\pi}$"
      ],
      "text/plain": [
       "1\n",
       "\n",
       "π"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1/pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2791a4c8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

Notice the "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA0AAAArCAYAAACjHgJXAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABPUlEQVRIDe2VMW7CQBBF7cABouQGHAFIbymhS5srwA2gtFtSpiRlKpLcAAqfICWlWzpER2neXy3WetdWonQgRvramdn5u57xaDYuyzJqkizL+vi/wAB978Z0XYPNW+x3sAND0AOB+CSd+KIoDpiy6LZAbgLPHxxXki3SGRfi3qZw5//v2G9Y2kdNKnkC6sUfUIAVewvWKCDJ+ZuccfVaU7vEnOI0TZtnWGsZrh1RleYSO+JfOVXvE0NjTnnGQMOkTRbETcxgsQQFroBID2AJ9BrKXoOIOE2mqAP02h1wvCVJUuR5/oz9gb1Bf0RfK5i9LbqRrmWbE6xvhO/V6rrRzDprm6VWCIJ1694J6ONz7ZCEZwKU10l6J8Vdq5s4UQmrep9uAH6N55pUJLwiFA2fM6oxMFySNmdewLdnG/MIWJ5aYR6r8pQAAAAASUVORK5CYII=\n",, which is a png of the LaTeX:

image

It does this by default as long as latex is installed.

This can be controlled by the use_latex flag. If it is True (the default), it generates both png and mathjax.

However, I don't know if it really should generate a png by default.

  • It is slow (it calls out to latex to generate the png with each output)
  • It makes the notebook file size much bigger than it otherwise would be
  • PNGs are generally bad for accessibility and look worse than natively rendered latex with something like mathjax.
  • Some notebook renderers will prefer them over latex, resulting in worse looking output. This is an upstream issue with jupyter, but it's been an issue forever.

I think we should change the default of use_latex to be 'mathjax'. The only benefit of png images is in renderers that don't support mathjax, like the qtconsole. We can detect if the frontend is the notebook and default to mathjax there and the current True otherwise.

@moorepants
Copy link
Member

I use the suggested default in my "Learn Multibody Dynamics" text: me.init_vprinting(use_latex='mathjax') because jupyter-sphinx will inject the pngs instead of mathjax for sympy output if you don't set it. See issue moorepants/learn-multibody-dynamics#95

One possible downside, is that when you aren't connected to the internet mathjax may not work. I'm not sure what happens when use_latex is mathjax and there is no internet.

@asmeurer
Copy link
Member Author

I just tested it and when I turn my internet off, the notebook can still render mathjax, because mathjax is installed locally in one of the jupyter packages.

However, I did try converting the notebook to html using nbconvert, and that does load mathjax from the CDN. If I turn my internet off, it just shows the raw latex. It doesn't seem to fallback to png, and as far as I can tell, the pngs aren't even saved there.

@moorepants
Copy link
Member

I've noticed (seemingly) that mathjax can sometimes be left in the browser cache and it works without internet but then sometimes it doesn't. But I'm mostly working with jupyter-sphinx which likely uses nbconvert under the hood.

@asmeurer
Copy link
Member Author

This was prompted by this issue jupyter-book/jupyter-book#1833. I don't know what the relationship is between jupyter-sphinx and jupyter-book, but with jupyter-book, notebooks with pngs saved in them will render the png instead of the latex.

@moorepants
Copy link
Member

It is possible the jupyter-book uses jupyter-sphinx. I'm not that familiar with jupyter-book though. Either way, they do similar things.

@moorepants
Copy link
Member

Looks like jupyter-book used to depend on jupyter-sphinx: https://github.com/search?q=org%3Aexecutablebooks+jupyter-sphinx&type=code

asmeurer added a commit to asmeurer/sympy that referenced this issue Jul 16, 2023
PNG output gets saved in notebooks, which makes their size much bigger, but it
isn't ever actually used because the MathJax LaTeX output is preferred.

I thought this might affect the qtconsole, which uses png output, but it
doesn't seem to. init_printing() wiht no arguments still enables png output
there. So the impact of this change should be minimal.

Fixes sympy#24983
@asmeurer
Copy link
Member Author

I have a fix at #25374. I thought this might break the qtconsole but it apparently still uses png even with this change.

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 a pull request may close this issue.

2 participants