Search code examples
python-sphinx

Sphinx: how to add a role and directive alias


I frequently use the :math: role and .. math:: directive, which is so frequent that I want to make a shorter alias m for them. How can I achieve it in Sphinx?


Solution

  • Add the following code snippet to conf.py. It defines a m directive and a m role that can be used as aliases of math:

    from sphinx.ext.mathbase import MathDirective, math_role
    
    def setup(app):
        app.add_directive('m', MathDirective)
        app.add_role('m', math_role)
    

    See also: