Skip to content

Instantly share code, notes, and snippets.

@fKunstner
Created August 9, 2018 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fKunstner/48c4e1024365c876ab644b20b0356554 to your computer and use it in GitHub Desktop.
Save fKunstner/48c4e1024365c876ab644b20b0356554 to your computer and use it in GitHub Desktop.
Math Macro support in Sphinx

Macro support in Math for Sphinx

A nice way to incorporate macros for use in math mode with MathJax should be available in the next version of Sphinx thanks to this pull request through the use The Sphinx options for MathJax configuration (Master) has already been updated

Given a mathCmds.tex command in the same folder as conf.py, adding the following lines to conf.py should make the macros in mathCmds.tex available in any document

import re

mathjax_config = {'Tex':{'Macros':{}}} # Create empty 

with open('mathCmds.tex', 'r') as f:
	for line in f:
		macros = re.findall(r'\\newcommand{\\(.*?)}(\[(\d)\])?{(.+)}', line)
		for macro in macros:
			if len(macro[1]) == 0:
				mathjax_config['Tex']['Macros'][macro[0]] = "{"+macro[3]+"}"
			else:
				mathjax_config['Tex']['Macros'][macro[0]] = ["{"+macro[3]+"}", int(macro[2])]

Todo

  • Add support for optional arguments in Regex (regex ref)

Links

\newcommand{\vA}{\mathbf{A}}
\newcommand{\Exp}[2]{\mathop{\mathbb{E}}_{#1}\left[#2\right]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment