0

I am using SymPy with PyScript so I cannot use init_printing() in my code as I am not running code cell by cell. How can I still use pprint to make my output look good in Web?

Output I am getting:

Current output

Output I am expecting:

Expected output

furas
  • 134,197
  • 12
  • 106
  • 148
  • you would have to convert it to `MathML` on your own. OR you would send it as `LaTeX` to web browser and use JavaScript libraray [mathjax](https://www.mathjax.org/) which will convert `LaTeX` to `MathML` directly in browser. – furas May 20 '22 at 16:07
  • it seems SymPy has methods `latex()` and `mathml()` to generate string with MathML or Latex: [printing - mathml](https://docs.sympy.org/latest/tutorial/printing.html#mathml) - which you can put in HTML with PyScript - but it may need to use directly access to JavaScript object instead of `print()` or `write()` - see [HTML Output in Pyscript](https://stackoverflow.com/questions/72197815/html-output-in-pyscript) – furas May 20 '22 at 16:22

1 Answers1

1

I wrote a MathJax example that displays formulas correctly: link Right-click on the page to view the source code.

The key is that you must tell MathJax to typeset the answer. This requires dropping down to JavaScript for a simple two line call:

<script>
function draw(str) {
    var math = MathJax.Hub.getAllJax("MathDiv")[0];
    MathJax.Hub.Queue([ "Text", math, str ]);
}
</script>

Example code including initializing MathJax and setting up the display DIV:

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<div id="MathDiv">\({}\)</div>

<py-script>
delta__y_l = symbols('Delta__y_l')
latexstr = latex(delta__y_l)
js.draw(latexstr)
</py-script>
John Hanley
  • 74,467
  • 6
  • 95
  • 159