mathjax_include.html 2.54 KB
Newer Older
1 2 3 4 5 6 7
##
## File:   templates/mathjax_include.html
##
## Advanced mathjax using 2.0-latest CDN for Dynamic Math
##
## This enables ASCIIMathJAX, and is used by js_textbox

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
 <script type="text/x-mathjax-config">

//  (function () {
    var QUEUE = MathJax.Hub.queue;  // shorthand for the queue
    var math = null;
    var jaxset = {};                // associative array of the element jaxs for the math output.
    var mmlset = {};		// associative array of mathml from each jax

      // constructs mathML of the specified jax element
        function toMathML(jax,callback) {
          var mml;
          try {
            mml = jax.root.toMathML("");
          } catch(err) {
            if (!err.restart) {throw err} // an actual error
            return MathJax.Callback.After([toMathML,jax,callback],err.restart);
          }
          MathJax.Callback(callback)(mml);
        }

      // function to queue in MathJax to get put the MathML expression in in the right document element
      function UpdateMathML(jax,id) {
              	toMathML(jax,function (mml) {
                    // document.getElementById(id+'_fromjs').value=math.originalText+ "\n\n=>\n\n"+ mml;
                    delem = document.getElementById("input_" + id + "_fromjs");
		    if (delem) { delem.value=mml; };
		    mmlset[id] = mml;
              	})
      }

MathJax.Hub.Config({
  tex2jax: {
    inlineMath: [
	["\\(","\\)"],
      ['[mathjaxinline]','[/mathjaxinline]']
    ],
   displayMath: [
	["\\[","\\]"],
     ['[mathjax]','[/mathjax]']
    ]
  }
});

    //
    //  The onchange event handler that typesets the
    //  math entered by the user
    //
    window.UpdateMath = function (Am,id) {
      QUEUE.Push(["Text",jaxset[id],Am]);
      QUEUE.Push(UpdateMathML(jaxset[id],id));
    }

//  })();

function DoUpdateMath(inputId) {
  var str = document.getElementById("input_"+inputId).value;

  // make sure the input field is in the jaxset
  if ($.inArray(inputId,jaxset) == -1){
      //alert('missing '+inputId);
    if (document.getElementById("display_" + inputId)){
	  MathJax.Hub.queue.Push(function () {
	      math = MathJax.Hub.getAllJax("display_" + inputId)[0];
	      if (math){
		  jaxset[inputId] = math;
	      }
	 });
      };
 }

  UpdateMath(str,inputId)
}

  </script>
  <%block name="headextra"/>

  <!-- This must appear after all mathjax-config blocks, so it is after the imports from the other templates -->
  <!-- TODO: move to settings -->
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-MML-AM_HTMLorMML-full">
</script>