math.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * A plugin which enables rendering of math equations inside
  3. * of reveal.js slides. Essentially a thin wrapper for MathJax.
  4. *
  5. * @author Hakim El Hattab
  6. */
  7. var RevealMath = window.RevealMath || (function(){
  8. var config = Reveal.getConfig().math || {};
  9. config.host = config.host || 'http://cdn.mathjax.org/mathjax/latest/MathJax.js';
  10. config.mode = config.mode || 'TeX-AMS_HTML-full';
  11. loadScript( config.host + '?config=' + config.mode, function() {
  12. MathJax.Hub.Config({
  13. messageStyle: 'none',
  14. tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] }
  15. });
  16. // Process any math inside of the current slide when navigating,
  17. // this is needed since it's not possible to typeset equations
  18. // within invisible elements (far future or past).
  19. Reveal.addEventListener( 'slidechanged', function( event ) {
  20. // This will only typeset equations that have not yet been
  21. // processed, as well as equations that have change since
  22. // last being processed.
  23. MathJax.Hub.Update( event.currentSlide );
  24. } );
  25. } );
  26. function loadScript( url, callback ) {
  27. var head = document.querySelector( 'head' );
  28. var script = document.createElement( 'script' );
  29. script.type = 'text/javascript';
  30. script.src = url;
  31. // Wrapper for callback to make sure it only fires once
  32. var finish = function() {
  33. if( typeof callback === 'function' ) {
  34. callback.call();
  35. callback = null;
  36. }
  37. }
  38. script.onload = finish;
  39. // IE
  40. script.onreadystatechange = function() {
  41. if ( this.readyState === 'loaded' ) {
  42. finish.call();
  43. }
  44. }
  45. // Normal browsers
  46. head.appendChild( script );
  47. }
  48. })();