python-sphinxmathjaxrestructuredtext

Left-align all math blocks in Sphinx HTML output


I have Sphinx documentation containing math blocks via the math directive. By default, they are centered in the HTML output. I want them all to appear aligned at the left, but having some offset with respect to the surrounding text.

For example, if this is in index.rst

For :math:`|r| < 1`:

.. math::

   \sum\limits_{k=0}^{\infty} r^k = \frac{1}{1-r}

Text in following paragraph put here to demonstrate that the math block
is in fact centered on the page.

and conf.py is completely empty, then running sphinx-build . output in the same folder produces the following HTML page as viewed in the browser:

rendered HTML

I want that formula and all other math blocks left-aligned with something like 2 "em" indentation. This question asks how to left-align a single math block. I'm hoping there is a way to achieve the same effect throughout the documentation without changing each and every math directive in the reStructuredText source.


Solution

  • This answer applies to MathJax, the default mathematics renderer in HTML output.

    The following setting in conf.py left-aligns all math directive content:

    mathjax3_config = {'chtml': {'displayAlign': 'left'}}
    

    Indentation can be customized with displayIndent:

    mathjax3_config = {'chtml': {'displayAlign': 'left',
                                 'displayIndent': '2em'}}
    

    References: