math.js 2.0 KB

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