Is it possible to hook in to the typesetting percent-complete number?

93 views
Skip to first unread message

Christian Perfect

unread,
Aug 22, 2012, 8:06:25 AM8/22/12
to mathja...@googlegroups.com
Sorry for doing the lazy thing and asking here before looking at the source code myself, but I have had a somewhat thorough look at the docs.

How is the typesetting percentage in the bar at the bottom-left calculated? Can I hook into it in any way? Ideally I'd like to register a callback function which gets sent the completion percentage (or the number of elements typeset and the total present on the page) every time a "New Math" signal is fired.

Christian

Davide P. Cervone

unread,
Aug 23, 2012, 8:12:02 AM8/23/12
to mathja...@googlegroups.com
The message is generated by the MathJax.Hub.processMessage() routine,
which is

processMessage: function (state,type) {
var m = Math.floor(state.i/(state.scripts.length)*100);
var message = (type === "Output" ? "Typesetting" : "Processing");
if (this.config.showProcessingMessages) {MathJax.Message.Set(message
+" math: "+m+"%",0)}
}

You can override that function in your configuration script if you
want. Something like

<script type="text/x-mathjax-config">
MathJax.Hub.processMessage = function (state,type) {
... do your thing here...
}
</script>

should do it.

It gets called periodically, though not for every math expression that
is processed (it is based on elapsed time, so the number of
expressions between calls depends on how complex they are). The time
between message updates is given by MathJax.Hub.processUpdateTime, so
if you made that 0, your routine would get called for every expression
that is typeset (but this will slow down the processing). There is
also MathJax.Hub.processUpdateDelay which is the tie to wait after
putting up the message before restarting the typesetting (to allow the
UI to respond to user interaction during typesetting). You might want
to reduce that if you end up making processUpdateTime smaller.

Finally, the 100% message is put up by hand after the typesetting is
done (it doesn't go through processMessage(), but probably should).
If you want to prevent that one, then set showProcessingMessages to
false in your configuration.

Davide

Christian Perfect

unread,
Aug 23, 2012, 8:48:09 AM8/23/12
to mathja...@googlegroups.com
Aha! I didn't think of just overriding the function. In fact, wrapping it would keep the MathJax functionality and let me do my stuff too. Thanks!

cp

On Thu, Aug 23, 2012 at 1:12 PM, Davide P. Cervone <dp...@union.edu> wrote:
The message is generated by the MathJax.Hub.processMessage() routine, which is

        processMessage: function (state,type) {
          var m = Math.floor(state.i/(state.scripts.length)*100);
          var message = (type === "Output" ? "Typesetting" : "Processing");
          if (this.config.showProcessingMessages) {MathJax.Message.Set(message+" math: "+m+"%",0)}

Davide P. Cervone

unread,
Aug 23, 2012, 8:56:15 AM8/23/12
to mathja...@googlegroups.com
Right.  That is one reason MathJax isolates some of its features like this, so it is easy to override them with your own versions.

For others who might se this later, one way to do that is the following:

<script type="text/x-mathjax-config">
(function () {
  var MESSAGE = MathJax.Hub.processMessage;  // save default action
  MathJax.Hub.processMessage = function (state,type) {
    ... your code here ...
            return MESSAGE.apply(MathJax.Hub,arguments);  // perform default action
  }
})()
</script>

In this particular case, you don't really need to return the value of the original routine (since it is null), but this gives the generic template for overriding a MathJax function.

Davide
Reply all
Reply to author
Forward
0 new messages