Seeking Help: Adapt linebreak feature with mhchemParser

25 views
Skip to first unread message

Magika Meoo

unread,
Apr 5, 2025, 2:56:03 PM4/5/25
to MathJax Users
I’m currently working on adapting the linebreak feature of MathJax4 for mhchemParser by adding\allowbreakbefore and after arrow and operator groups in the_mhchemTexifyprocess. However, mhchemParser adds outer braces to each converted tex string by default, and it break the linebreak feature. I’m considering removing these outer braces, yet I’m unsure what bad case might pop up. Additionally, I’m wondering if there are some way to make the feature work with the braces. example: a\allowbreak b {a\allowbreak b}

Davide Cervone

unread,
Apr 10, 2025, 8:14:22 AM4/10/25
to mathja...@googlegroups.com
There is no easy way to make inline line-breaking work within braces (display math can still break within the braces, so I assume you are talking about in-line breaks).  You can remove the braces using the configuration

MathJax = {
  loader: {load: ['[tex]/mhchem']},
  tex: {packages: {'[+]': ['mhchem']}},
  startup: {
    ready() {
      const ce = MathJax._.input.tex.MapHandler.MapHandler.getMap('mhchem').lookup('ce');
      const machine = ce._func;
      function myMachine(parser, name, type) {
        machine(parser, name, type);
        let mhchem = parser.GetArgument(name);
        parser.string = mhchem + ' ' + parser.string.slice(parser.i);
        parser.i = 0;
      }
      ce._func = myMachine;
      MathJax.startup.defaultReady();
    }
  }
}

I don't think there is any loss to doing this in MathJax.  

Note that you do not need to use \allowbreak for things like \ce{A -> B} if you do this, as the \longrightarrow that is used here is already a valid breakpoint for inline breaks (at least for before the arrow, you may want one after it).

Davide


On Apr 5, 2025, at 2:56 PM, Magika Meoo <golden...@gmail.com> wrote:

I’m currently working on adapting the linebreak feature of MathJax4 for mhchemParser by adding\allowbreakbefore and after arrow and operator groups in the_mhchemTexifyprocess. However, mhchemParser adds outer braces to each converted tex string by default, and it break the linebreak feature. I’m considering removing these outer braces, yet I’m unsure what bad case might pop up. Additionally, I’m wondering if there are some way to make the feature work with the braces. example: a\allowbreak b {a\allowbreak b}

--
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 visit https://groups.google.com/d/msgid/mathjax-users/62266115-2854-4285-a39d-4cabd37917bbn%40googlegroups.com.

Magika Meoo

unread,
Apr 10, 2025, 9:04:41 AM4/10/25
to MathJax Users
I'm not very familiar with LaTeX. I wonder why these braces are added in mhchem. Is this some kind of LaTeX convention? It doesn't seem to affect the rendering result.

Davide Cervone

unread,
Apr 10, 2025, 9:53:42 AM4/10/25
to mathja...@googlegroups.com
In actual LaTeX, they may be useful in keeping the result of \ce and the other similar macros as a single item.  LaTeX has many more commands that MathJax, including ones that expand macros before reforming other commands, and that might be useful in those situations.  MathJax doesn't hav those macros, so it doesn't really make a difference here, but the author has used basically the same code for both the javascript and LaTeX implementations.

Davide

Reply all
Reply to author
Forward
0 new messages