Forum Discussion

SamHill's avatar
SamHill
Super Hero
2 months ago

Math Equations Storyline (MathJax/LaTex)

Here's a method for adding accessible Math Equations to your Storyline project using the MathJax JavaScript library.

----------------------------------------------------

Load the MathJax library from the content delivery network (CDN) into the Slide Master of your project. Add this script to the "timeline starts" trigger. This will ensure the library processes equations on any slide throughout your module. This could be modified to load the script file locally, if you copy the "tex-mml-chtml.js" into your published files, and load from there instead.

function loadMathJax() {
	const script = document.createElement('script');
	script.src = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js";
	script.async = true;
	// Find the last script in the head
	const lastScript = document.head.getElementsByTagName('script');
	const lastScriptElement = lastScript[lastScript.length - 1];
	// Insert the new script after the last script
	if (lastScriptElement) {
	  lastScriptElement.parentNode.insertBefore(script, lastScriptElement.nextSibling);
	} else {
	  document.head.appendChild(script);
	}
	// set loaded flag;
	window.MathJaxLoaded = true;
  }
  if(typeof window.MathJaxLoaded === "undefined") loadMathJax();

Define your equation as your usually would in the text box:

The ALT text value needs to be edited into a special syntax so that it gets processed by MathJax:

When the slide loads, the contents of the slide is processed by MathJax, which results in the following, accessible, formatted equation. The added bonus is that the non accessible version of the equation is hidden from screen readers using the aria-hidden="true" attribute + value.

Consider the following equation 
<mjx-container class="MathJax CtxtMenu_Attached_0" jax="CHTML" display="true" tabindex="0" ctxtmenu_counter="0" style="font-size: 114.6%; position: relative;">
	<mjx-math display="true" class="MJX-TEX" aria-hidden="true" style="margin-left: 0px; margin-right: 0px;">
		<mjx-msup>
			<mjx-mi class="mjx-i">
				<mjx-c class="mjx-c1D44E TEX-I"></mjx-c>
			</mjx-mi>
			<mjx-script style="vertical-align: 0.413em;">
				<mjx-mn class="mjx-n" size="s">
					<mjx-c class="mjx-c32"></mjx-c>
				</mjx-mn>
			</mjx-script>
		</mjx-msup>
		<mjx-mo class="mjx-n" space="3">
			<mjx-c class="mjx-c2B"></mjx-c>
		</mjx-mo>
		<mjx-msup space="3">
			<mjx-mi class="mjx-i">
				<mjx-c class="mjx-c1D44F TEX-I"></mjx-c>
			</mjx-mi>
			<mjx-script style="vertical-align: 0.413em;">
				<mjx-mn class="mjx-n" size="s">
					<mjx-c class="mjx-c32"></mjx-c>
				</mjx-mn>
			</mjx-script>
		</mjx-msup>
		<mjx-mo class="mjx-n" space="4">
			<mjx-c class="mjx-c3D"></mjx-c>
		</mjx-mo>
		<mjx-msup space="4">
			<mjx-mi class="mjx-i">
				<mjx-c class="mjx-c1D450 TEX-I"></mjx-c>
			</mjx-mi>
			<mjx-script style="vertical-align: 0.413em;">
				<mjx-mn class="mjx-n" size="s">
					<mjx-c class="mjx-c32"></mjx-c>
				</mjx-mn>
			</mjx-script>
		</mjx-msup>
	</mjx-math>
	<mjx-assistive-mml unselectable="on" display="block">
		<math
			xmlns="http://www.w3.org/1998/Math/MathML" display="block">
			<msup>
				<mi>a</mi>
				<mn>2</mn>
			</msup>
			<mo>+</mo>
			<msup>
				<mi>b</mi>
				<mn>2</mn>
			</msup>
			<mo>=</mo>
			<msup>
				<mi>c</mi>
				<mn>2</mn>
			</msup>
		</math>
	</mjx-assistive-mml>
</mjx-container>

 

No RepliesBe the first to reply