How can I render AsciiMath expressions in both inline and display style with MathJax 3?

31 views
Skip to first unread message

Frank Illenberger

unread,
Feb 15, 2024, 6:42:29 PMFeb 15
to MathJax Users
By default, MathJax3 is rendering AsciiMath expressions in inline style. In my project, I need AsciiMath expressions also in display style. As a start, I tried to achieve this by subclassing FindAsciiMath:

class MyFindAsciiMath extends MathJax._.input.asciimath.FindAsciiMath.FindAsciiMath {
  getPatterns() {
    let options = this.options
    let starts = []
    this.end = {}
    options['delimiters'].forEach((delims) => this.addPattern(starts, delims, true))
    this.start = new RegExp(starts.join('|'), 'g')
    this.hasPatterns = (starts.length > 0)
  }
}

Here I set the 3rd parameter of addPattern to true to switch it to display mode. But this only results in the mjx-assistive-mml element getting a block display attribute but the expression is still rendered inline. Is there a way to achieve this with MathJax3? In MathJax2 this used to work with postfilterHooks.

Many thanks.

Davide Cervone

unread,
Feb 17, 2024, 6:09:57 PMFeb 17
to mathja...@googlegroups.com
The current AsciiMath input jax is really the v2 one pasted into MathJax v3 with some shims and glue functions, and it really needs to be made into a proper input jax for v3.  It's on the to-do list, but other things have taken priority.  It turns out that the display parameter doesn't actually work properly, as you have noticed.

Here is a configuration that will do what is needed:

<script>
MathJax = {
  loader: {load: ['input/asciimath', 'output/chtml']},
  asciimath: {
    delimiters: [['``','``'], ['`','`']]
  },
  startup: {
    ready() {
      const {AsciiMath} = MathJax._.input.asciimath_ts;
      Object.assign(AsciiMath.prototype, {
        _compile: AsciiMath.prototype.compile,
        compile(math, document) {
          math.display = (math.start?.delim === '``');
          const result = this._compile(math, document);
          const mstyle = result.childNodes[0].childNodes.pop();
          mstyle.childNodes.forEach(child => result.appendChild(child));
          if (math.display) {
            result.attributes.set('display', 'block');
          }
          return result;
        }
      });
      MathJax.startup.defaultReady();
    }
  }
};
</script>

Here, we configure AsciiMath to use two different delimiters (the order matters), and hijack the AsciiMath compile() method to handle our needed display mode.  We remove the top-level mstyle element that AsciiMath adds to set the displaystyle, and set the math element's display attribute to block for display mode (leaving it unset for in-line math, since that is the default).  This makes displayed math be a displayed equation as well as typesetting in display style.  If you want it to remain an in-line expression but using display style rules, change

            result.attributes.set('display', 'block');

to

            result.attributes.set('displaystyle', 'true');

instead.

I think this should do what you are looking for.

Davide


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mathjax-users/b8f9ed47-a3ac-4d6e-ab0b-a79a05a1a7bdn%40googlegroups.com.

Frank Illenberger

unread,
Feb 21, 2024, 4:52:41 AMFeb 21
to mathja...@googlegroups.com
Thank you very much!

Reply all
Reply to author
Forward
0 new messages