math.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 loaded = false;
  9. var config = Reveal.getConfig().math || {};
  10. config.host = config.host || 'http://cdn.mathjax.org/mathjax/latest/MathJax.js';
  11. config.mode = config.mode || 'TeX-AMS_HTML-full';
  12. loadScript( config.host + '?config=' + config.mode, function() {
  13. // Conditioned just in case both onload and readystate fire
  14. if( loaded === false ) {
  15. loaded = true;
  16. MathJax.Hub.Config({
  17. messageStyle: 'none',
  18. tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] }
  19. });
  20. // Process any math inside of the current slide when navigating,
  21. // this is needed since it's not possible to typeset equations
  22. // within invisible elements (far future or past).
  23. Reveal.addEventListener( 'slidechanged', function( event ) {
  24. // This will only typeset equations that have not yet been
  25. // processed, as well as equations that have change since
  26. // last being processed.
  27. MathJax.Hub.Update( event.currentSlide, function() {
  28. Reveal.layout();
  29. } );
  30. } );
  31. }
  32. } );
  33. function loadScript( url, callback ) {
  34. var head = document.querySelector( 'head' );
  35. var script = document.createElement( 'script' );
  36. script.type = 'text/javascript';
  37. script.src = url;
  38. // Wrapper for callback to make sure it only fires once
  39. var finish = function() {
  40. if( typeof callback === 'function' ) {
  41. callback.call();
  42. callback = null;
  43. }
  44. }
  45. script.onload = finish;
  46. // IE
  47. script.onreadystatechange = function() {
  48. if ( this.readyState === 'loaded' ) {
  49. finish.call();
  50. }
  51. }
  52. // Normal browsers
  53. head.appendChild( script );
  54. }
  55. })();