Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suppress errors about multiple sub/superscripts? #3196

Open
hbghlyj opened this issue Feb 26, 2024 · 1 comment
Open

suppress errors about multiple sub/superscripts? #3196

hbghlyj opened this issue Feb 26, 2024 · 1 comment
Labels
Code Example Contains an illustrative code example, solution, or work-around Question v3

Comments

@hbghlyj
Copy link

hbghlyj commented Feb 26, 2024

Is your feature request related to a problem? Please describe.
Similar question to Is there a way to override LaTeX's errors about double subscripts and superscripts?

When one writes

a^b^c, a_b_c

or

a^b'

MathJax gives an error message complaining about multiple super/subscripts.

Is there a way to override the error and have MathJax output

a^{bc} a_{bc} {a^b}'  

Describe the solution you'd like
Is there a way to redefine ^ and _ to append an empty {}? like in LaTeX:

\catcode`\^ = 13 \def^#1{\sp{#1}{}}
\catcode`\_ = 13 \def_#1{\sb{#1}{}}
@hbghlyj hbghlyj changed the title Is there a way to override MathJax's errors about double subscripts and superscripts? suppress errors about multiple sub/superscripts? Feb 26, 2024
@dpvc
Copy link
Member

dpvc commented Mar 3, 2024

It can be done using the following configuration:

MathJax = {
  tex: {
    packages: {'[+]': ['my-scripts']}
  },
  startup: {
    ready() {
      const {Configuration} = MathJax._.input.tex.Configuration;
      const {CommandMap, MacroMap} = MathJax._.input.tex.SymbolMap;
      const BaseMethods = MathJax._.input.tex.base.BaseMethods.default;
      
      new MacroMap('my-script-chars', {
        '^': ['Macro', '\\sp{#1}{}', 1],
        '_': ['Macro', '\\sb{#1}{}', 1],
        '\'': 'Prime',
      }, {
        Macro: BaseMethods.Macro,
        Prime(parser, c) {
          const top = parser.stack.Top()
          const [base, atom] = top?.Peek(2) || [];
          if (base && atom && 
              base.isKind('msubsup') && !base.isKind('msup') && !!base.childNodes[base.sup] &&
              atom.isKind('TeXAtom') && atom.childNodes[0].childNodes.length === 0) {
            top.Pop(); top.Pop();
            const msup = parser.create('node', 'msup', [base.childNodes[0], base.childNodes[base.sup]]);
            parser.stack.Push(msup);
          }
          BaseMethods.Prime(parser, c);
        }
      });
      
      new CommandMap('my-script-cs', {
        sp: 'Superscript',
        sb: 'Subscript'
      }, BaseMethods);
      
      Configuration.create('my-scripts', {
        handler: {
          macro: ['my-script-cs'],
          character: ['my-script-chars']
        }
      });
      MathJax.startup.defaultReady();
    }
  }
}

Note, however, that a^b^c doesn't actually give a^{bc} but rather a^{b}{}^{c}{}, which is semantically different, though visually similar, so people with screen readers will get a strange result from this. Similarly for a_b_c, which gives a_{b}{}_{c}.

In actual TeX, these do not render quite the same:

tex

(note the extra space in the second form). MathJax v3 gets this wrong and both appear identical, but v4 fixes the error and produces the result shown above. There is a similar extra space for the subscripts (in LaTeX and v4), and in the result for {a^b}' that is missing in v3.

@dpvc dpvc added Question v3 Code Example Contains an illustrative code example, solution, or work-around labels Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Example Contains an illustrative code example, solution, or work-around Question v3
Projects
None yet
Development

No branches or pull requests

2 participants