Mathematical diagrams and macro support [tfmt-000G]
- January 7, 2023
- Jon Sterling
A basic requirement of tools for scientific thought is to support the rendering of mathematical diagrams. What kinds of diagrams are needed depends, of course, on the problem domain: for my own work, the main diagram-forms needed are commutative diagrams and string diagrams.
Mathematical expressions and diagrams are tightly coupled [tfmt-000P]
- January 7, 2023
- Jon Sterling
Although diagramming may seem to non-mathematicians to be somewhat orthogonal to notational macro support, in reality any solution to the diagramming problem must be tightly and natively integrated with the rendering of mathematical expressions — simply because most diagrams involve mathematical expressions, and these invariably involve notational macros. The reason PGF/TikZ has been so successful is that it respects this tight coupling.
The situation for hypertext mathematical tools is somewhat less advanced than that of LaTeX and PFG/TikZ, but there are several options which we discuss below.
Commutative diagrams in KaTeX [tfmt-000J]
- January 7, 2023
- Jon Sterling
\KaTeX has a very rudimentary support for commutative diagrams built-in, by emulating the amscd package. Unfortunately, this support is completely inadequate for usage by mathematicians today:
- Only square diagram shapes are supported: commutative diagrams in general have diagonal and curved lines, but these are not supported.
- The rendering of the limited gamut of supported commutative diagrams is broken in most browsers (at least Safari and Firefox). In particular, lines are jagged as they are pieced together from pipes and arrows that are subtly misaligned.
Commutative diagrams in MathJax [tfmt-000K]
- January 7, 2023
- Jon Sterling
Like \KaTeX , MathJax supports the amscd commands for rudimentary square-shaped commutative diagrams. Unlike the \KaTeX implementation of amscd, the supported diagrams are rendered correctly without jagged lines; this means that for the vanishingly small population of mathematicians whose needs are limited to square-shaped diagrams, MathJax’s builtin support is viable.
On the other hand, there is a more advanced option available for users of MathJax: the XyJax-v3 plugin, which adds support for the full gamut of xypic diagrams to MathJax. Notably, this plugin is used by the Stacks Project. The only downside of the xypic support is that it interacts poorly with accessibility features (but no worse than any other solution to rendering non-trivial commutative diagrams), and diagrams created using xypic look considerably less professional than those created using tikz or quiver.
Both \KaTeX and MathJax have the benefit that diagrams created using them will respect the ambient macro package with which the tool has been configured; therefore, if one looks past the rudimentary nature of the support for commutative diagrams, our main requirement is indeed satisfied. Another tool worth discussing is quiver.
The quiver interactive diagramming tool [tfmt-000I]
- January 7, 2023
- Jon Sterling
The quiver application is an excellent graphical interface for interactively constructing commutative diagrams, with very high-quality rendering.
One positive aspect of quiver is that it is possible to load it with your own macro library, so that diagrams involving custom notations render correctly in the graphical interface. The downside of the approach here is that the macro library must be located on a publicly accessible URL that can be pasted into the quiver interface.
Quiver also offers excellent support for embedding the resulting diagrams in existing LaTeX documents: after creating your diagram, you can request a LaTeX snippet that includes a URL which allows you to resume editing your diagram. For example, the following code corresponds to the URL https://q.uiver.app/?q=WzAsMixbMCwwLCJBIl0sWzEsMCwiQiJdLFswLDFdXQ==:
% https://q.uiver.app/#q=WzAsMixbMCwwLCJBIl0sWzEsMCwiQiJdLFswLDFdXQ==
\[\begin{tikzcd}
A & B
\arrow[from=1-1, to=1-2]
\end{tikzcd}\]Unfortunately, the support for embedding quiver diagrams in HTML documents is currently inadequate for professional use. The HTML embed code provided simply produces an <iframe>, and it is not possible to style the interior of the embedded frame (e.g. to change the background color, or decrease the margins):
<!-- https://q.uiver.app/?q=WzAsMixbMCwwLCJBIl0sWzEsMCwiQiJdLFswLDFdXQ== --> <iframe class="quiver-embed" src="https://q.uiver.app/?q=WzAsMixbMCwwLCJBIl0sWzEsMCwiQiJdLFswLDFdXQ==&embed" width="304" height="176" style="border-radius: 8px; border: none;"></iframe>
Therefore, we must conclude that although quiver is an excellent tool for authors of traditional LaTeX documents, it is not currently a candidate for inclusion in tools for hypertext mathematical authoring.
Because of the currently inadequate support of quiver for embedding diagrams in hypertext settings, we cannot consider it any further. There is a final option that turns out to be the most used in practice: generating SVG images statically from embedded LaTeX code.
Generating images statically using LaTeX [tfmt-000L]
- January 7, 2023
- Jon Sterling
Because of the general inadequacy of the other available tools, most authors of hypertext mathematics with diagramming needs tend to rely on the static generation of images from LaTeX code using a local LaTeX toolchain. It is not difficult to instrument pandoc with a Lua filter to render tikz code to SVG images.
There are also a variety of other tools that do something similar, which tend to be employed in static site generation:
- antex by Paolo Brasolin is used by Krater as well as jekyll-sheafy, both via jekyll-antex.
- Forester by Jonathan Sterling is used by the present web site.
The basic architecture of such a tool is to scan for LaTeX blocks, and then identify them by a hash of their contents. This hash is used as a filename for .tex files, which are compiled to .dvi and thence to .svg using the dvisvgm tool; the resulting file is then embedded in HTML using an <img> tag. Alternatively, is also possible to transclude the resulting <svg> element directly, but then one must be careful to rename all identifiers in the <svg> element uniquely, as it is possible for two different <svg> elements on a single page to interfere one each other.
Both antex and Forester support passing a macro library to be used when rendering. Both jekyll-sheafy and Forester set their macro libraries on a page-local basis.
A serious downside of generating images from LaTeX code is the negative impact on accessibility tools. This seems only slightly mitigated by the transclusion of the <svg> element as opposed to using <img>. Ultimately accessibility for mathematical diagrams remains an unsolved problem, and it does not seem that the existing discussion on accessibility of hypertext mathematics has much to say about this problem.
Finally, we comment on more principled approaches using web standards such as SVG and MathML that we hope will take form in the future.
SVG is not an authoring language [tfmt-000N]
- January 7, 2023
- Jon Sterling
SVG is an extremely powerful low-level language for vector images and diagrams with a variety of applications. Unfortunately, it is not reasonable to compose such diagrams directly in SVG as an author: in contrast to programmatic tools like PGF/TikZ, all positions in SVG are fixed, and there is no possibility to impose important abstractions (e.g. the concept of a line that is “glued” to a pair of nodes). On the other hand, there are many advantages to SVG, including the possibility to intermix SVG with other formats such as MathML.
Because of the low level of abstraction, SVG images that appear in practice today are nearly always produced by a tool or compiler from an input that is defined at a much higher level of abstraction.
MathML is not an authoring language [tfmt-000O]
- January 7, 2023
- Jon Sterling
Despite some preliminary support for structured representation of high-level mathematical idioms via Content MathML, MathML is not intended to be an authoring language: it is a target language for other tools. Moreover, the content dictionaries (collections of basic elements) of Content MathML are chosen to pertain to the needs of grade-school and secondary-school mathematics and not at all to the needs of professional mathematics:
The base set of content elements is chosen to be adequate for simple coding of most of the formulas used from kindergarten to the end of high school in the United States, and probably beyond through the first two years of college, that is up to A-Level or Baccalaureate level in Europe.
Nonetheless, it seems that the goal was for the content dictionaries of Content MathML to be extended by the individual “communities of practice” to meet their specific needs:
Hence, it is not in general possible for a user agent to automatically determine the proper interpretation for
definitionURLvalues without further information about the context and community of practice in which the MathML instance occurs.
However, in contexts where highly precise semantics are required (e.g. communication between computer algebra systems, within formal systems such as theorem provers, etc.) it is the responsibility of the relevant community of practice to verify, extend or replace definitions provided by OpenMath CDs as appropriate.
It seems that there is a possibility to use XSLT to define your own semantic notational macros, and this certainly bears further investigation. Due to the mutually reinforcing combination of historically poor vendor support and near-absolute isolation from actual communities of practice, i.e. working mathematicians, sophisticated direct use of MathML has never caught on. On the other hand, there is a great deal of MathML on the web today in the form of MathJax and \KaTeX output — tools which are not only currently necessary for obtaining consistent (and professional-quality) rendering of mathematics across browsers, but also are necessary for authoring due to their more succinct markup and easy support for macros.
It seems that the future of MathML is brighter than it was in the past, as we are finally seeing a vital project to improve vendor support led by Igalia. Currently, even browsers that support the MathML standard do so with completely inadequate and unprofessional rendering quality, which means that tools like MathJax and \KaTeX may remain necessary for some time even after vendors finally support MathML. But we hope that with improved vendor support comes new and productive experiments with using semantic tools like XSLT to handle macros, etc. Unfortunately, given the tight coupling between the authoring of mathematical expressions and of mathematical diagrams, this transformation will not take place unless high-level hypertext-compatible tools for drawing diagrams are simultaneously developed.
Towards mixing SVG and MathML in hypertext mathematics [tfmt-000M]
- January 7, 2023
- Jon Sterling
The W3C MathML Core Working Draft points out that MathML can be embedded into <svg> elements using the <foreignObject> element. This is a great strength of the modularity of the model, and I believe that in the future, we will be able to use this as a way to render accessible mathematical diagrams in hypertext.
What is missing? Essentially the current issue preventing widespread use of this method is the fact that neither SVG nor MathML is an authoring language: they are both currently too low-level to be seriously used by authors.
For exactly so long as diagrams must be drawn using LaTeX-based tools rather than something MathML-compatible, it would be non-negotiable for the support of notational macros to itself be based in LaTeX syntax (e.g. as in both \KaTeX and MathJax). But it is worth imagining a future in which mathematical diagrams are drawn using a high-level interface to SVG, and then a pure MathML approach to notational macros becomes quite viable. This is not currently the world we live in, but it is something to hope for.