Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Sphinx with MathJax to use dollar signs for math delimiter

How can I configure Sphinx to use dollar signs as a math delimiter with the MathJax extension?

I tried adding

mathjax_config = {
    'tex2jax': {
      'inlineMath': [ ['$','$']],
      'displayMath': [ ['$$','$$']],
    },
}

to conf.py, but that has two issues:

  1. Math requires two backslashes. For instance, $\\sin(x)$. It seems Sphinx strips single backslashes.
  2. If I don't include a normal ..math directive on the page somewhere, it doesn't load the MathJax Javascript. I want to exclusively use $ and $$ to delimit math.
like image 643
asmeurer Avatar asked Sep 19 '25 15:09

asmeurer


1 Answers

I've written a Sphinx extension that lets you do this https://www.sympy.org/sphinx-math-dollar/

To use it, install the extension

pip install sphinx-math-dollar

and add it to your conf.py

extensions = ['sphinx_math_dollar', 'sphinx.ext.mathjax']

As a technical note, trying to do this correctly without using this extension will be quite difficult. You have to do the replacement at just the right place in the Sphinx processing. If you do it too late in the processing (e.g., by setting the JavaScript config), it won't work because Sphinx will have already stripped all backslashes from the math at that point. If you do it too soon (e.g., by doing a regex replacement on the input text), it will also fail because you will end up replacing math in things that you don't want to, for instance, in doctests or in code.

like image 114
asmeurer Avatar answered Sep 21 '25 07:09

asmeurer