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

Erroneous "Erroneous nesting of equation structures" #3070

Open
cebola2 opened this issue Jul 20, 2023 · 9 comments
Open

Erroneous "Erroneous nesting of equation structures" #3070

cebola2 opened this issue Jul 20, 2023 · 9 comments
Labels
Accepted Issue has been reproduced by MathJax team Code Example Contains an illustrative code example, solution, or work-around Merged Merged into develop branch Test Needed v4
Milestone

Comments

@cebola2
Copy link

cebola2 commented Jul 20, 2023

I believe I have found some error in the "Erroneous nesting of equation structures" flagging when using \begin{equation}\end{equation}

Version
MathJax Version:4.0.0-beta.3

I am loading MathJax locally and the configuration is identical to what I had for version 4.0.0-alpha.1, namely

window.MathJax = {
output: {
font: 'mathjax-fira',
linebreaks: {// options for when overflow is linebreak
inline: true, // true for browser-based breaking of inline equations
width: '100%', // a fixed size or a percentage of the container width
lineleading: .2, // the default lineleading in em units
LinebreakVisitor: null // The LinebreakVisitor to use
}
},
loader: {load: ['ui/lazy']},
svg: {
scale: 1,
mtextInheritFont: true,
displayOverflow: "linebreak",
matchFontHeight: false
},
chtml: {
scale: 1,
mtextInheritFont: true,
displayOverflow: "linebreak"
},
options: {
processHtmlClass: "math",
ignoreHtmlClass: "tex2jax_ignore"
},
tex: {
tags: 'ams',
macros: {
sen: "\operatorname{sen}",
tg: "\operatorname{tg}",
arctg: "\operatorname{arctg}",
arcsen: "\operatorname{arcsen}",
arcos: "\operatorname{arcos}",
div: "\operatorname{div}",
rot: "\operatorname{rot}"
},
inlineMath: [['$', '$'], ['\(', '\)']],
displayMath: [['$$', '$$'], ["\[", "\]"]],
processEscapes: true,
autoload: {
color: ['color']
}
}
/* tex: {
packages: {'[+]': ['noerrors', 'action']}
},
loader: {
load: ['[tex]/noerrors', '[tex]/action']
} */
};

Example page:
https://cdi2.palhoto.pt/test

@dpvc dpvc added the Accepted Issue has been reproduced by MathJax team label Jul 20, 2023
@dpvc
Copy link
Member

dpvc commented Jul 20, 2023

OK, thanks for the report. I will need to look into it. The logic for handling the nesting of AMS structures has been changed (see the last paragraph of the New TeX Features in the release notes), and obviously something went wrong.

@dpvc dpvc added the v4 label Jul 20, 2023
@dpvc
Copy link
Member

dpvc commented Jul 20, 2023

OK, I found it. There is a bug in

https://github.com/mathjax/MathJax-src/blob/49db078a876cce1f2157c9c58fb98bb145a09068/ts/input/tex/base/BaseMethods.ts#L1709-L1722

where parser.Push(begin) is called twice; the first call should have been removed. This may have been due to a bad merge or bad conflict resolution, as the first one was removed in the commit that modified the nesting checks:

mathjax/MathJax-src@7bb5fbd#diff-8b54d4a07ba56e1e6678245687ace65d468dd87cb4c8770a11b261c94d0231a0

as you can see by the first difference line above.

In any case, the following configuration will fix the error:

MathJax = {
  tex: {
    packages: {'[+]': ['fix-equation']}
  },
  startup: {
    ready() {
      const {Configuration} = MathJax._.input.tex.Configuration;
      const {EnvironmentMap} = MathJax._.input.tex.SymbolMap;
      const {environment} = MathJax._.input.tex.ParseMethods.default;
      const ParseUtil = MathJax._.input.tex.ParseUtil.default;
      new EnvironmentMap('fix-equation', environment, {
        displaymath:   ['Equation', null, false],
        math:          ['Equation', null, false, false],
        equation:      ['Equation', null, true],
        `equation*`:   ['Equation', null, false],
      }, {
        Equation(parser, begin, numbered, display = true) {
          parser.configuration.mathItem.display = display;
          parser.stack.env.display = display;
          ParseUtil.checkEqnEnv(parser);
          parser.Push(begin);
          return parser.itemFactory.create('equation', numbered).
          setProperty('name', begin.getName());
        }
      });
      Configuration.create('fix-equation', {
        handler: {environment: ['fix-equation']}
      });
      MathJax.startup.defaultReady();
    }
  }
};

Merge this into your current configuration (e.g., move the packages option into your existing tex block and add the startup block) and that should take care of it for you.

Thanks for trying out beta.3 and reporting the problem.

@cebola2
Copy link
Author

cebola2 commented Jul 20, 2023

The same test page with the indicated fix:

https://cdi2.palhoto.pt/test_5

Everything works! Thanks for your incredible work with MathJax.

@dpvc
Copy link
Member

dpvc commented Jul 20, 2023

Glad it is working for you now. Thanks for letting us know.

@egnha
Copy link

egnha commented Jul 26, 2023

@dpvc Thank you for the fix. However, while it works for most environments, it doesn't seem to resolve the issue for equation* environments (which worked as expected in v3).

Example:

<html>
<head>
  <script>
    MathJax = {
      tex: {
        packages: {'[+]': ['fix-equation']}
      },
      startup: {
        ready() {
          const {Configuration} = MathJax._.input.tex.Configuration;
          const {EnvironmentMap} = MathJax._.input.tex.SymbolMap;
          const {environment} = MathJax._.input.tex.ParseMethods.default;
          const ParseUtil = MathJax._.input.tex.ParseUtil.default;
          new EnvironmentMap('fix-equation', environment, {
            displaymath:   ['Equation', null, false],
            math:          ['Equation', null, false, false],
            equation:      ['Equation', null, true]
          }, {
            Equation(parser, begin, numbered, display = true) {
              parser.configuration.mathItem.display = display;
              parser.stack.env.display = display;
              ParseUtil.checkEqnEnv(parser);
              parser.Push(begin);
              return parser.itemFactory.create('equation', numbered).
              setProperty('name', begin.getName());
            }
          });
          Configuration.create('fix-equation', {
            handler: {environment: ['fix-equation']}
          });
          MathJax.startup.defaultReady();
        }
      }
    };
  </script>
  <script src="https://cdn.jsdelivr.net/npm/mathjax@4.0.0-beta.3/tex-mml-chtml.js">
  </script>
</head>
<body>
  This works:

  \begin{equation}
  A=B
  \end{equation}

  As does this:

  \[
  A=B
  \]

  But not this:

  \begin{equation*}
  A=B
  \end{equation*}
  
</body>
</html>

This renders as follows (in several major browsers):
Screenshot 2023-07-26 at 05 32 07

(I don't know JS. Have I implemented your fix incorrectly?)

@dpvc
Copy link
Member

dpvc commented Jul 26, 2023

OK, sorry, I missed equation*, since it is in a different file from the others. Add

            'equation*':      ['Equation', null, false]

after the corresponding definition for equation in your configuration (and add a comma after the equation line) and that should take care of it.

@egnha
Copy link

egnha commented Jul 26, 2023

Thanks for your suggestion. However, should `equation*' be 'equation*'? Either way, the error persists. (And equation* as quoted in the first instance also breaks the equation environment.)

@dpvc
Copy link
Member

dpvc commented Jul 26, 2023

should `equation*' be 'equation*'?

Yes. Sorry about that.

Either way, the error persists.

That suggests you made an error in the configuration and the configuration isn't being processed or is processed in correctly. Here is the full configuration from my example

MathJax = {
  tex: {
    packages: {'[+]': ['fix-equation']}
  },
  startup: {
    ready() {
      const {Configuration} = MathJax._.input.tex.Configuration;
      const {EnvironmentMap} = MathJax._.input.tex.SymbolMap;
      const {environment} = MathJax._.input.tex.ParseMethods.default;
      const ParseUtil = MathJax._.input.tex.ParseUtil.default;
      new EnvironmentMap('fix-equation', environment, {
        displaymath:   ['Equation', null, false],
        math:          ['Equation', null, false, false],
        equation:      ['Equation', null, true],
        'equation*':   ['Equation', null, false]
      }, {
        Equation(parser, begin, numbered, display = true) {
          parser.configuration.mathItem.display = display;
          parser.stack.env.display = display;
          ParseUtil.checkEqnEnv(parser);
          parser.Push(begin);
          return parser.itemFactory.create('equation', numbered).
          setProperty('name', begin.getName());
        }
      });
      Configuration.create('fix-equation', {
        handler: {environment: ['fix-equation']}
      });
      MathJax.startup.defaultReady();
    }
  }
};

This works for me in my test program. I've edited the previous example as well, so others who find this will get the correct behavior from either example.

@egnha
Copy link

egnha commented Jul 27, 2023

That works now for me, too. Thank you!

@dpvc dpvc added the Code Example Contains an illustrative code example, solution, or work-around label Jul 31, 2023
dpvc added a commit to mathjax/MathJax-src that referenced this issue Aug 18, 2023
Fix incorrect flagging of nested environments. (mathjax/MathJax#3070)
@dpvc dpvc added Merged Merged into develop branch and removed Ready for Review labels Aug 18, 2023
@dpvc dpvc added this to the v4.0 milestone Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Issue has been reproduced by MathJax team Code Example Contains an illustrative code example, solution, or work-around Merged Merged into develop branch Test Needed v4
Projects
None yet
Development

No branches or pull requests

3 participants