Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbered :math: equations in reStructuredText

How can I make an equation in restructured text, that is followed by an equation number:

p = f(x)                (1)

.. math::
   p = f(x)

would only lead to:

p = f(x)
like image 273
Davoud Taghawi-Nejad Avatar asked Dec 07 '22 10:12

Davoud Taghawi-Nejad


1 Answers

Looking at this a few years later, it appears that the number is still not automatically placed on the right of the equations. I'd therefore like to supplement the accepted answer a bit.

First you add a label to the equation in the rst file:

.. math::
   :label: pfx

   p = f(x)

This will generate a <span> of class eqno containing the number and link to the equation. To make it show up as you would expect an equation number to show up, you need to add a style to override the default behavior of that class.

I usually do this by adding custom.css to the _static/css folder under my doc root:

.math {
    text-align: left;
}
.eqno {
    float: right;
}

The math class labels the <div> containing the whole equation. Without the text-align: left; style, all your equations would be centered, making it totally reasonable to number them on the left.

Now you need to register the CSS file in conf.py. I've added the following basic hook:

def setup(app):
    app.add_stylesheet('css/custom.css')

Relative paths are resolved from the _static folder. You can add globbing to pick up all the files in the folder at once, but this should suffice.

The reason that @EngineeredBrain's comment reports a number on the previous line is that their equations are too long and don't fit on the same line. I'm sure there is a way to style them to fit no matter what, but I won't even attempt to go into that here.

This will only work in sphinx (not rST), and only with HTML output as far as I'm aware. I'll try with latexpdf one day and update.

like image 89
Mad Physicist Avatar answered Dec 28 '22 06:12

Mad Physicist