presentations

Presentations
Log | Files | Refs

math.js (2160B)


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