math.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 options = Reveal.getConfig().math || {};
  9. options.mathjax = options.mathjax || 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js';
  10. options.config = options.config || 'TeX-AMS_HTML-full';
  11. options.tex2jax = options.tex2jax || {
  12. inlineMath: [['$','$'],['\\(','\\)']] ,
  13. skipTags: ['script','noscript','style','textarea','pre'] };
  14. loadScript( options.mathjax + '?config=' + options.config, function() {
  15. MathJax.Hub.Config({
  16. messageStyle: 'none',
  17. tex2jax: options.tex2jax,
  18. skipStartupTypeset: true
  19. });
  20. // Typeset followed by an immediate reveal.js layout since
  21. // the typesetting process could affect slide height
  22. MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub ] );
  23. MathJax.Hub.Queue( Reveal.layout );
  24. // Reprocess equations in slides when they turn visible
  25. Reveal.addEventListener( 'slidechanged', function( event ) {
  26. MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, event.currentSlide ] );
  27. } );
  28. } );
  29. function loadScript( url, callback ) {
  30. var head = document.querySelector( 'head' );
  31. var script = document.createElement( 'script' );
  32. script.type = 'text/javascript';
  33. script.src = url;
  34. // Wrapper for callback to make sure it only fires once
  35. var finish = function() {
  36. if( typeof callback === 'function' ) {
  37. callback.call();
  38. callback = null;
  39. }
  40. }
  41. script.onload = finish;
  42. // IE
  43. script.onreadystatechange = function() {
  44. if ( this.readyState === 'loaded' ) {
  45. finish();
  46. }
  47. }
  48. // Normal browsers
  49. head.appendChild( script );
  50. }
  51. })();