math.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. function defaults( options, defaultOptions ) {
  21. for ( var i in defaultOptions ) {
  22. if ( !options.hasOwnProperty( i ) ) {
  23. options[i] = defaultOptions[i];
  24. }
  25. }
  26. }
  27. function loadScript( url, callback ) {
  28. var head = document.querySelector( 'head' );
  29. var script = document.createElement( 'script' );
  30. script.type = 'text/javascript';
  31. script.src = url;
  32. // Wrapper for callback to make sure it only fires once
  33. var finish = function() {
  34. if( typeof callback === 'function' ) {
  35. callback.call();
  36. callback = null;
  37. }
  38. }
  39. script.onload = finish;
  40. // IE
  41. script.onreadystatechange = function() {
  42. if ( this.readyState === 'loaded' ) {
  43. finish();
  44. }
  45. }
  46. // Normal browsers
  47. head.appendChild( script );
  48. }
  49. return {
  50. init: function() {
  51. defaults( options, defaultOptions );
  52. defaults( options.tex2jax, defaultOptions.tex2jax );
  53. options.mathjax = options.config = null;
  54. loadScript( url, function() {
  55. MathJax.Hub.Config( options );
  56. // Typeset followed by an immediate reveal.js layout since
  57. // the typesetting process could affect slide height
  58. MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub ] );
  59. MathJax.Hub.Queue( Reveal.layout );
  60. // Reprocess equations in slides when they turn visible
  61. Reveal.addEventListener( 'slidechanged', function( event ) {
  62. MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, event.currentSlide ] );
  63. } );
  64. } );
  65. }
  66. }
  67. })();
  68. Reveal.registerPlugin( 'math', RevealMath );