Numbered :math: equations in reStructuredText

纵饮孤独 提交于 2020-06-27 14:48:49

问题


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)

回答1:


.. math:: p=f(x)
  :label: eq:pfx



回答2:


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.



来源:https://stackoverflow.com/questions/14110790/numbered-math-equations-in-restructuredtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!