mathjax / MathJax-demos-web

A repository with examples using mathjax-v3
https://mathjax.github.io/MathJax-demos-web
Apache License 2.0
254 stars 106 forks source link

Encapsulate part of equation into its own DOM element #21

Closed gcalmettes closed 4 years ago

gcalmettes commented 4 years ago

I am trying to rewrite an old mathjax extension (adding Reveal.js specific capability to mathjax equations rendered into the slides) using the new API of Mathjax V3.

I am using the custom-tex-extension example as starting point.

The goal would be to be able to encapsulate part of an equation into its own DOM element on which specific classes have been added.

See the image below for an example: in this case, I am aiming at isolating the \alpha + 4 part into its own DOM element for which I am adding a danger class. What I get instead is on the right part.

ex

The problem I have is that right now, the part is correctly isolated, but the \alpha + 4 part is not translated and only the raw text is rendered. This is not surprising since I am giving parser.create('text', text) as child, but I was unable to find how to add the "transformed" nodes instead.

A simplified version of the relevant code is given below.

tex_fragments(parser, name, type) {
    const def = parseAttributes(parser.GetBrackets(name))

    const text = convertEscapes(parser.GetArgument(name))

    const node = parser.create('node', 'mi', [parser.create('text', text)], def)
    parser.Push(node)

}

Any guidance on how I could do this would be appreciated. Thanks a lot and great work on the V3!

gcalmettes commented 4 years ago

Solved! Found the parser.ParseArg method that is what I needed in this case.

tex_fragments(parser, name, type) {
    const def = parseAttributes(parser.GetBrackets(name))

    const math = parser.ParseArg(name )
    const node = parser.create('node', 'mrow', [math], def)

    parser.Push(node)

}