Search code examples
documentationrestructuredtext

rST: Inserting a code-block in a table


I need to create a table in rST using the same format as used by the DVB specifications.

The syntax column is similar to a code block in that it needs to have proper indentation, but it also needs to align with the other columns.

I tried various ways... the issue seems that leading whitespace does strange things to the rST.

+-----------------------------+-----------------+------------+
| Syntax                      | Number of bits  | Identifier |
+=============================+=================+============+
|``private_data_byte(){``     |                 |            |
+-----------------------------+-----------------+------------+
|``  magic_bit``              | 8               | uimsbf     |
+-----------------------------+-----------------+------------+
|``  version``                | 8               | uimsbf     |
+-----------------------------+-----------------+------------+
|``  if (a == 0x0F){``        |                 |            |
+-----------------------------+-----------------+------------+
|``    field_a``              | 16              | uimsbf     |
+-----------------------------+-----------------+------------+
|``  }``                      |                 |            |
+-----------------------------+-----------------+------------+
|``}``                        |                 |            |
+-----------------------------+-----------------+------------+

Solution

  • A workaround is to use non-breaking space defined by a substitution:

    .. |_| unicode:: 0xA0 
    
    +-----------------------------+-----------------+------------+
    | Syntax                      | Number of bits  | Identifier |
    +=============================+=================+============+
    |``private_data_byte(){``     |                 |            |
    +-----------------------------+-----------------+------------+
    | |_| ``magic_bit``           | 8               | uimsbf     |
    +-----------------------------+-----------------+------------+
    | |_| ``version``             | 8               | uimsbf     |
    +-----------------------------+-----------------+------------+
    | |_| ``if (a == 0x0F){``     |                 |            |
    +-----------------------------+-----------------+------------+
    | |_| |_| ``field_a``         | 16              | uimsbf     |
    +-----------------------------+-----------------+------------+
    | |_| ``}``                   |                 |            |
    +-----------------------------+-----------------+------------+
    |``}``                        |                 |            |
    +-----------------------------+-----------------+------------+