Skip to content

Margins around MathJax equations in WordPress

Love the MathJax plugin for WordPress. Makes it really easy to insert equations, assuming you’re somewhat familiar with Latex formatting.

Problem is, there is always no space below the equation. I’ve been just hitting enter a couple of times after the closing latex tag, but I wanted a better solution.

Since there’s a syntax attribute as part of the latex tag, I decided to add my own. If I set syntax to block, the plugin now wraps the equation in a div that has a bottom margin of 32px.

function latex_shortcode($atts,$content)  {
	self::$add_script = true;
	//this gives us an optional "syntax" attribute, which defaults to "inline", but can also be "display"
				'syntax' => get_option('latex_syntax'),
			), $atts));
	if ($syntax == 'inline') {
		return "\(" . $content . "\)";
	else if ($syntax == 'block') {
		return "<div style=\"margin-bottom:32px;\">\(" . $content . "\)</div>";
	else if ($syntax == 'display') {
		return "<div style=\"margin-bottom:32px;\">\[" . $content . "\]</div>";

I changed the latex_shortcode function a bit. Added another check for $syntax == ‘block’, and if true then wrap the output in a div. Also use the same div settings for the display syntax, although I don’t really like the display syntax because it centers the equation.

Published inWordPress

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *