Skip to content

Hooking in to the typesetting percent complete number

Davide P. Cervone edited this page May 26, 2013 · 1 revision

From https://groups.google.com/d/msg/mathjax-users/yk0gj6UC-KM/MRzZakbnvGYJ


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


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 time 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


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


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

Clone this wiki locally