Math mode pretty print Sage cell in newly compiled PTX documents

37 views
Skip to first unread message

kcri...@gmail.com

unread,
Jun 10, 2021, 10:36:32 PM6/10/21
to PreTeXt support
I hope this is just some obscurity of my setup.  However, when I try the following code in a Sage cell at e.g. https://pretextbook.org/examples/sample-article/html/section-sage-cells.html I just get an empty output:

pretty_print(html("$a^2$"))

This happens in both Safari and FF.  It works fine in older compiled PTX documents, and it works fine on the Sage cell server, so it perhaps has something to do with Mathjax 3 and the Sage pretty_print function combining.  I hope this is helpful - I personally use this feature a lot, but I might be the only one.

Alex Jordan

unread,
Jun 10, 2021, 10:53:17 PM6/10/21
to pretext...@googlegroups.com
Sage is producing this HTML:

<script type="math/tex">a^2</script>

When MathJax is in a script tag, it needs a special configuration in
MathJax3 to actually render it. We have that in place now, but only
when there are "webwork" on the page.
> --
> You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-support/e0dc80f0-7fd6-4b49-bbaa-9a3fe03554a0n%40googlegroups.com.

Rob Beezer

unread,
Jun 10, 2021, 11:35:55 PM6/10/21
to pretext...@googlegroups.com
Dang - I was going to shoot in the dark and suggest replacing the $, $ by \(,
\). Good catch, Alex.

Any reason not to turn this on universally? And this may even become our
default for HTML output.

https://github.com/rbeezer/mathbook/issues/611

Alex Jordan

unread,
Jun 11, 2021, 12:28:32 AM6/11/21
to pretext...@googlegroups.com
It seems likely that the sage html() is producing output with the
expectation that MathJax2 will process it. With MJ2, these script tags
were always processed automatically (because MJ2 actually turned
delimited math into these script tags as the first step). So it might
be worth exploring if there is anything to do on the sage end to make
it act more in a natural MJ3 way. Why does:
html("$a^2$")
even recognize that as math content at all and produce
<script type="math/tex">a^2</script>
?
Somewhere inside the guts of html(), some specific MathJax processing
is happening, and maybe can be configured.

But it should be harmless to turn it on universally. Or to be more
cautious, change the conditional here:
https://github.com/rbeezer/mathbook/blob/2393e58d7e6a68402f22d8b02ce4c3d45c7b84f9/xsl/pretext-html.xsl#L11088
to also be true when sage is on the page.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-support/e1e48981-afd3-e0af-6f45-37f18e4ca78b%40ups.edu.

kcri...@gmail.com

unread,
Jun 11, 2021, 8:21:39 AM6/11/21
to PreTeXt support

Thanks to everyone for the quick action on this - I'm sure any of these solutions will do.

This lives in sage.misc.html where if there is neither an _html_ nor _latex_ method (e.g. a raw string) then it uses math_parse, whose doctoring says the following.  Do you think this MJ3 change should be reported to Sage as a possible issue for them?   https://github.com/sagemath/sage/blob/develop/src/sage/misc/html.py#L65

    Turn the HTML-ish string s that can have \$\$ and \$'s in it into

    pure HTML.  See below for a precise definition of what this means.


    INPUT:


    - ``s`` -- a string


    OUTPUT:


    A :class:`HtmlFragment` instance.


    Do the following:


    * Replace all ``\$ text \$``\'s by

      ``<script type="math/tex"> text </script>``

    * Replace all ``\$\$ text \$\$``\'s by

      ``<script type="math/tex; mode=display"> text </script>``

    * Replace all ``\ \$``\'s by ``\$``\'s.  Note that in

      the above two cases nothing is done if the ``\$``

      is preceeded by a backslash.

    * Replace all ``\[ text \]``\'s by

      ``<script type="math/tex; mode=display"> text </script>``


Alex Jordan

unread,
Jun 11, 2021, 10:56:48 AM6/11/21
to pretext...@googlegroups.com
I opened:
https://github.com/rbeezer/mathbook/pull/1498

which would make the original example work in current PreTeXt.

I don't really know what to advise for the sage people. Maybe bring it
up for discussion, showing a simple example web page that is using
MathJax3?
> --
> You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-support/7dfcf39b-424a-42f3-9614-d5a3de198211n%40googlegroups.com.

Rob Beezer

unread,
Jun 11, 2021, 1:39:57 PM6/11/21
to pretext...@googlegroups.com
On 6/11/21 5:21 AM, kcri...@gmail.com wrote:
>
> Thanks to everyone for the quick action on this - I'm sure any of these
> solutions will do.

Should be merged later today.

> Do you think this MJ3 change should be reported to Sage as a
> possible issue for them?

Definitely. The interpretation of a "script" element by MJ was automatic for
MJ2, and now requires nontrivial configuration in MJ3. So I'd think both the
Sage Cell Server and the use of Sage in a Jupyter notebook would require
adjustments.

Rob

Rob Beezer

unread,
Jun 11, 2021, 6:04:40 PM6/11/21
to pretext...@googlegroups.com
Alex's quick fix has been merged, and I added KDC's example into the sample
article at

https://pretextbook.org/examples/sample-article/html/section-sage-cells.html#p-19

Details via

https://github.com/rbeezer/mathbook/pull/1498

Rob

kcri...@gmail.com

unread,
Jun 12, 2021, 9:57:12 AM6/12/21
to PreTeXt support
See https://trac.sagemath.org/ticket/31517 where this may have been fixed upstream.  But that is actually in 9.3, apparently, and yet we had this problem?

kcri...@gmail.com

unread,
Jun 14, 2021, 7:53:43 AM6/14/21
to PreTeXt support
Another interesting followup from William Stein regarding MJ3 (https://trac.sagemath.org/ticket/25833#comment:9).  I don't know if this is going to be a problem for incompatibility or not, as I don't know the internals of MJ2 or 3.

+++

It's worth paying attention to the fact that Mathjax 3 is missing a significant feature that might be really critical for Sage, which is "automatic line breaking". See the discussion here:

​https://github.com/mathjax/MathJax/issues/2312#issuecomment-692695446

E.g., "So line breaking and other features will most likely have to wait until next year." "That's the reason why nearly nobody is using MathJax? 3." etc.

I think this is very important to be aware of, and it's one reason why CoCalc? doesn't use MathJax? 3, and uses KaTeX by default. For some applications automatic line breaking probably isn't so important, but it might be very important for Sage, due to people often printing out polynomials and big formulas.

David Farmer

unread,
Jun 14, 2021, 9:02:57 AM6/14/21
to PreTeXt support

I think one should distinguish between line breaking and line wrapping.

For a large polynomial, or the first many terms of a power series,
it it usually is safe to wrap the symbols onto multiple lines, where the
line break occurs between terms. The key word is "safe", which I
will not define.

Expressions with a complicated structure cannot safely be split across
lines in an automatic way.

MathJax has some reasonable heuristics which enable one to browse the
hierarchical structure of the expression, allowing one to see for
example that the giant expression is one fraction. Then one can
explore the numerator and denominator separately, each of which may have
its own internal structure.

However, this topic is only relevant in terms of how Sage cells work
in PreTeXt documents, which now uses MathJax3.

It is just a matter of turning on the MathJax3 feature of processing
the contents of those script tags? (Apologies if that question has
already been answered.)

David
> --
> You received this message because you are subscribed to the Google Groups "PreTeXt support"
> group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> pretext-suppo...@googlegroups.com.
> To view this discussion on the web visithttps://groups.google.com/d/msgid/pretext-support/5b48e0fe-4d44-4f0f-8588-d722286554ffn%40g
> ooglegroups.com.
>
>

Rob Beezer

unread,
Jun 14, 2021, 11:31:22 AM6/14/21
to pretext...@googlegroups.com
On 6/12/21 6:57 AM, kcri...@gmail.com wrote:
> See https://trac.sagemath.org/ticket/31517 where this may have been fixed
> upstream.  But that is actually in 9.3, apparently, and yet we had this problem?

I don't understand where MJ enters into the processing when the Sage Cell is
used. Earlier, I looked at the page source for the primary Sage Cell demo page,
thinking I might see it being loaded there (thinking I'd see MJ2).

So I don't understand how/why the MJ3 loaded into a page produced by PreTeXt
seems to "take over." At least that seems to be the case, given that adding
support for the "script" element made the original example render properly, and
that the html() construction clearly creates a "script" element in vanilla Sage
(independent of the Cell server). Perhaps "pretty_print()" runs MJ server-side
for the Sage Cell server? Which still would not explain everything to me.

Rob

kcri...@gmail.com

unread,
Jun 15, 2021, 8:46:28 AM6/15/21
to PreTeXt support
 

I don't understand where MJ enters into the processing when the Sage Cell is
used. Earlier, I looked at the page source for the primary Sage Cell demo page,
thinking I might see it being loaded there (thinking I'd see MJ2).

So I don't understand how/why the MJ3 loaded into a page produced by PreTeXt
seems to "take over." At least that seems to be the case, given that adding
support for the "script" element made the original example render properly, and
that the html() construction clearly creates a "script" element in vanilla Sage
(independent of the Cell server). Perhaps "pretty_print()" runs MJ server-side
for the Sage Cell server? Which still would not explain everything to me.


For what it's worth, see https://groups.google.com/g/sage-cell/c/CQHchYvttWc where Andrey says he already upgraded to MJ3 on the cell server (presumably this means the page itself).  Maybe that doesn't go for embeddings? 
Reply all
Reply to author
Forward
0 new messages