Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 764
mac Avatar asked Sep 08 '11 12:09

mac


People also ask

How do I align columns to the right?

Use width:100% on the table and the middle column. You could also set width:100% on the last column's style and td align="right" on the last column. Then you can insert more columns in the middle while the spacing still works.

How do I align a column in a table?

Select the table chart, click in the corner of a table column, and then click in the column formatting bar until you reach the desired alignment. The control shows the alignment you are about to set. Click to center the text. Click to align the text to the right.

How do you align columns?

On the Home tab, click Paragraph, and then click Align. Select the Align with option and then select the paragraph tag pertaining to the column one paragraph. Click OK.

How do you align columns in a table in HTML?

Table data defaults to left alignment; table headers to center. In order to change the alignment in one cell, insert the appropriate "ALIGN=" attribute within the code for that cell. In order to change the alignment in all cells in a row, insert the appropriate alignment attribute within the code for that row.


1 Answers

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.

like image 133
Eli Collins Avatar answered Sep 19 '22 07:09

Eli Collins