Subject: 30.0.90; Math in EWW/shr

From: Augusto Stoffel <[email-masked]>
Date: Sat, 05 Oct 2024 16:01:36 +0200

shr has a tendency to display garbled math formulas.  For example
opening https://developer.mozilla.org/en-US/docs/Web/MathML/Examples/MathML_Pythagorean_Theorem
in EWW shows this:

> if a and b are the legs and c is the hypotenuse, then a2+b2=c2a^2 +
> b^2 = c^2.

This happens because <math> HTML elements tend to contain a MathML
description of the formula and possibly some "annotations", so what we
see above is "a2+b2=c2", the naive rendering of the MathML, concatenated
with "a^2 + b^2 = c^2" which is an annotation saying what the formula is
in TeX notation.

So in as a first improvement I would like to suggest adding this
definition:

  (defun shr-tag-math (dom)
    (shr-generic (thread-first
                   dom
                   (dom-child-by-tag 'semantics)
                   (dom-child-by-tag 'annotation)
                   (or dom))))

The above page then gets rendered like this:

> Specifically, if a and b are the legs and c is the hypotenuse, then
> a^2 + b^2 = c^2.

In other words, only the TeX annotation is displayed, which is as good
as it gets when trying to show math as plain text, IMO.

It is possible for the <math> element to include a SVG rendition of the
formula.  We could consider displaying that if available (not sure how
common it is, though).  Also, we might consider including infrastructure
to render MathML, but this would require external programs (there is no
reason to reinvent MathJax).  I've done that in one of my packages, see
this commit for the relevant code:

https://github.com/astoff/devdocs.el/commit/04a8793072f1fc7c23f115e2c7ee83ce669b6fa4

(Actually, I added a version of the above to two of my packages already,
so I think it's pretty clear Emacs should have some general
math-rendering infrastructure.)