Search code examples
pythonalignmentpython-sphinxrestructuredtexttabular

How to right-align columns content in reStructuredText simple tables?


I'm editing the documentation for a project of mine using Sphinx, which in turn uses reStructuredText as markup language.

I have a simple table (as opposed to grid table) in which the rightmost column reports contains numbers that I would like to right-align, but I couldn't find how to achieve that.

============  =====================
Event               Score variation
============  =====================
Event 1                        +100
Event 2                         -25
Event 3                        -400
============  =====================

I would be happy to switch to a grid table if this would allow me to solve the problem.


Solution

  • Sadly I don't think rst offers that ability... the table styling options are rather limited. That said, if you're rendering to HTML, you could add a custom stylesheet with a css rule such as:

    table.right-align-right-col td:last-child {
        text-align: right
    }
    

    and then add the directive:

    .. rst-class:: right-align-right-col
    

    right above your table in the rst file. It's clunky, but it should work.


    update 2013-2-6: I've since needed to accomplish this myself, and came up with a more permanent solution. The cloud_sptheme.ext.table_styling Sphinx extension adds directives for doing column alignment, per-column css classes, and number of other table styling tricks. Despite being packaged as part of the "cloud" Sphinx theme, it should work with any Sphinx theme.