typst / typst/typst

Embedding SVGs in MathML

Open
#8,727 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request html math
Dominant language
Rust
Stars
56.1k
Forks
1.7k
Avg merge
3d 10h
Merged PRs (30d)
22

Description

Description

I am adding some HTML support to curryst: a package that produces diagrams that are useful both in and outside of math mode. I'd like to target the SVG format, rather than pure MathML, as since these proof trees are diagrams, it makes sense for them to be downloadable and portable at the cost of losing the ability to copy+paste. (See also https://github.com/lynn/typst-syntree/pull/23).

However, SVGs that are directly embedded in MathML do not render. They must be wrapped in a <semantics> and an <annotation-xml> block in somewhat of a kludgy fashion, "annotating" a non-existent MathML block. This is a little gross but idiomatic.

<math display="block">
  <mi mathvariant="normal">Π</mi>
  <mspace width="1em"></mspace>
  <mo lspace="0em" rspace="0em">=</mo>
  <mspace width="1em"></mspace>
  <semantics>
    <!-- this section intentionally left blank -->
    <annotation-xml encoding="image/svg+xml">
      <svg> ... </svg>
    </annotation-xml>
  </semantics>
</math>

Unfortunately, I have hit two issues in my attempts to add this support.

  1. There is no way (to my knowledge) to introspect upon whether some code is within math mode or within markup mode, without the use of show math.equation and gratuitous global state (a non-starter for just adding HTML support). I'd like to be able to do something like what can be currently done with context target() == "html".
  2. Typst's html.elem does not support outputting reserved XML (?) names.
error: name is reserved and not valid for a custom element
    ┌─ curryst.typ:498:16
    │
498 │       html.elem("annotation-xml", attrs: (encoding: "image/svg+xml"), html.frame(rendered-tree))
    │                 ^^^^^^^^^^^^^^^^

The second issue might be a genuine issue, but the first issue I'm hitting smacks of an XY problem -- I think what I'd rather just have happen, is for Typst to wrap any html.frame in <semantics><annotation-xml encoding="image/svg+xml">...</annotation-xml></semantics> when in math mode. So I'd like to open a feature request for this. But also... if there is a better way to approach this problem, I'd love to hear it.

Use Case

(see above)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the html.elem and context target() entry points, then compare how show math.equation exposes math-mode state. Determine whether HTML frames in math mode should receive the proposed semantics and annotation-xml wrapper, and how reserved XML names should be handled. Done means the SVG MathML use case works without global state and the behavior is covered by the relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.