disable hiding preRemoveClass on errors in math

25 views
Skip to first unread message

Вадим Дворовенко

unread,
Feb 16, 2015, 12:50:33 AM2/16/15
to mathja...@googlegroups.com
Hello. I'm trying to migrate from TexLive rendering png to MathJax on moodle server. We've got a lot of equations in tests, so we cannot one day switch off TexLive and us mathjax, because there are a lot of situations, where mathjax currently fails (e.g. \vline, or like this https://groups.google.com/d/msg/mathjax-users/ZMBTo5b1kRg/PyicYpVttoMJ).

Currenlty moodle puts png image in preview span and equations into script tag like described in docs for preRemoveClass:
<span class="MathJax_Preview"><img ...></span><script type="math/tex">...</script>

MathJax always hides preview after load. Is there a way to hide preview only in cases, when equation is rendered by mathjax without errors and warnings. And in case when mathjax failes to show equation, just to show preview span and not show any mathjax error.

Peter Krautzberger

unread,
Feb 16, 2015, 11:27:23 AM2/16/15
to mathja...@googlegroups.com
Hi,

Not a direct answer but have you considered using MathJax-node to check for processing errors server-side? That seems more natural for this particular use case.

Regards,
Peter.

--
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.
For more options, visit https://groups.google.com/d/optout.

Вадим Дворовенко

unread,
Feb 16, 2015, 11:51:47 AM2/16/15
to mathja...@googlegroups.com
Not a direct answer but have you considered using MathJax-node to check for processing errors server-side? That seems more natural for this particular use case.

Thank you Peter, for your idea, but it's too hard to implement. Moodle is php app and i do not think i should add nodejs to make it work better. I just need client-side mathjax rendering with fallback to server-side texlive rendering, which alredy works good.
As for mathjax-node, i don't think it's stable enough. I've tried to set it up on Windows, Ubuntu and RedHat, only on redhat examples worked out of the box, on windows and ubuntu example just returned empty doc.
When i tried to process 1000 equations with mathjax-node on redhat, it breaks, and i do not have enough nodejs experience to debug such problems.

I'm looking much simplier solutuion - some config option for mathjax, which disables hiding preview (alredy processed by texlive) if there are any errors in procesing.
I've found option not to show errors, but i cannot find option not to hide preview.

Peter Krautzberger

unread,
Feb 16, 2015, 11:55:03 AM2/16/15
to mathja...@googlegroups.com
Hi,

Sorry to hear you couldn't get mathjax-node to run. It would be helpful if you could file bugs for the issues you've had. (There's a current thread regarding Windows issues but Ubuntu is a tested target OS.)

Anywya, there's no built-in way of doing this client-side. You'll have to roll your own extension for this.

Regards,
Peter.


--
Message has been deleted

Вадим Дворовенко

unread,
Feb 17, 2015, 10:45:11 PM2/17/15
to mathja...@googlegroups.com
I've finally solved this problem. Maybe not very pretty solution. but it works for me.
As far as preview removal is hardcoded in process, i disable preview removal and hide preview only on no errors. I'm disabling noErrors and noUndefined to revert to preview on any problem.

MathJax.Hub.Typeset_source = MathJax.Hub.Typeset;
MathJax.Hub.Typeset = function(node, callback) {
  return MathJax.Hub.Typeset_source(node, function() {
    var jax = MathJax.Hub.getAllJax(node);
    for (var i = 0, m = jax.length; i < m; i++) {
      var math = jax[i];
      if (!math.texError) {
        var pre = math.SourceElement().previousSibling.previousSibling;
        if (pre && pre.className !== "MathJax_Preview") {
          pre = pre.previousSibling;
        };
        if (pre && pre.className === "MathJax_Preview") {
          pre.innerHTML = "";
        };
      } else {
        var pre = math.SourceElement().previousSibling;
        if (pre && pre.className === "MathJax") {
          pre.innerHTML = "";
        };
      };
    };
    return callback;
  });
};

Some test cases
<span class="MathJax_Preview"><img src="https://upload.wikimedia.org/math/5/8/4/5844a6bcc33147716ef10bca7465e3e7.png"></span><script type="math/tex">\nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial\mathbf{D}}{\partial t}</script>

<span class="MathJax_Preview"><img src="https://upload.wikimedia.org/math/5/8/4/5844a6bcc33147716ef10bca7465e3e7.png"></span><script type="math/tex">\ERROR \nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial\mathbf{D}}{\partial t}</script>

<span class="MathJax_Preview"><img src="https://upload.wikimedia.org/math/5/8/4/5844a6bcc33147716ef10bca7465e3e7.png"></span><script type="math/tex">\nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial</script>


Full demo HTML
<!DOCTYPE html>
<html>
<head>
<title>Fallback to preview</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: {
    noErrors: { disabled: true },
    noUndefined: { disabled: true }
  },
  preRemoveClass: "MathJax_Preview_Alternative"
});

MathJax.Hub.Typeset_source = MathJax.Hub.Typeset;
MathJax.Hub.Typeset = function(node, callback) {
  return MathJax.Hub.Typeset_source(node, function() {
    var jax = MathJax.Hub.getAllJax(node);
    for (var i = 0, m = jax.length; i < m; i++) {
      var math = jax[i];
      if (!math.texError) {
        var pre = math.SourceElement().previousSibling.previousSibling;
        if (pre && pre.className !== "MathJax_Preview") {
          pre = pre.previousSibling;
        };
        if (pre && pre.className === "MathJax_Preview") {
          pre.innerHTML = "";
        };
      } else {
        var pre = math.SourceElement().previousSibling;
        if (pre && pre.className === "MathJax") {
          pre.innerHTML = "";
        };
      };
    };
    return callback;
  });
};
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>

<p>

Correct:

<span class="MathJax_Preview"><img src="https://upload.wikimedia.org/math/5/8/4/5844a6bcc33147716ef10bca7465e3e7.png"></span><script type="math/tex">\nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial\mathbf{D}}{\partial t}</script>

<script type="math/tex">\( \nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial\mathbf{D}}{\partial t} \)</script>

</p>

<p>

Errors with preview fallback:

<span class="MathJax_Preview"><img src="https://upload.wikimedia.org/math/5/8/4/5844a6bcc33147716ef10bca7465e3e7.png"></span><script type="math/tex">\ERROR \nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial\mathbf{D}}{\partial t}</script>

<span class="MathJax_Preview"><img src="https://upload.wikimedia.org/math/5/8/4/5844a6bcc33147716ef10bca7465e3e7.png"></span><script type="math/tex">\nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial</script>

</p>

<p>

Errors without preview fallback:

<script type="math/tex">\ERROR \nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial\mathbf{D}}{\partial t}</script>

<script type="math/tex">\nabla\times\mathbf{H}=\frac{4\pi}{c} \mathbf{j}+\frac{1}{c}\frac{\partial</script>

</p>

</body>
</html>




Take a look, maybe it should be posted on mathjax-docs/wiki/
Reply all
Reply to author
Forward
0 new messages