math.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.mathjax = config.mathjax || 'http://cdn.mathjax.org/mathjax/latest/MathJax.js';
  10. config.dialect = config.dialect || 'TeX-AMS_HTML-full';
  11. loadScript( config.mathjax + '?config=' + config.dialect, function() {
  12. MathJax.Hub.Config({
  13. messageStyle: 'none',
  14. tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] },
  15. skipStartupTypeset: true
  16. });
  17. // Typeset followed by an immediate reveal.js layout since
  18. // the typesetting process could affect slide height
  19. MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub ] );
  20. MathJax.Hub.Queue( Reveal.layout );
  21. // Reprocess equations in slides when they turn visible
  22. Reveal.addEventListener( 'slidechanged', function( event ) {
  23. MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, 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();
  43. }
  44. }
  45. // Normal browsers
  46. head.appendChild( script );
  47. }
  48. })();