Math equations in drupal

Forums: 

Hi. I have been trying to get MathJax to work in Drupal but not having much luck. Is there another way or another Maths editor I can use? It works for question labels but not for anything rendered within the question. I have added the following code:

function h5p_node_view($node, $view_mode) {
	// If not teaser and has library.
	if (isset($node->main_library)) {
	  // Add generic CSS (will not work on iframed content).
	  $module_path = drupal_get_path('module', 'h5p');
	  if (H5PCore::determineEmbedType($node->embed_type, $node->main_library['embedTypes']) === 'div') {
		// Div
		drupal_add_html_head('<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=T..."></script>');
	  }
	  else {
		// Iframe
		$content_id = h5p_get_content_id($node);
		$settings['h5p']['cid-' . $content_id]['scripts'][] = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=T...';
		$settings['h5p']['cid-' . $content_id]['styles'][] = $module_path . '/css/styles.css';
		drupal_add_js($settings, 'setting');
	  }
	}
}

 

Attachments: 
serettig's picture

You need to add JavaScript code that runs the MathJax rendering process whenever the content of the H5P element changes. As all H5P content types change the DOM all the time, this is absolutely necessary.

What must be included has been discussed in several forum posts.

There is a very recent one that included modified H5P content types: https://h5p.org/node/226089 However, the approach taken there means that you can't use the standard content types and that equations will only display in the modified ones.

I've taken a similar approach in this GitHub project: https://github.com/sr258/MathJax

If you want to search for the forum post, you it was concerned with MathJax and 'hooks'.

Actually you can use the standard content types, if you add the H5P.KaTeX dependency to the standard content types. The only downside is that it will be overwritten by an update of the content type.