使用重构文本网格表设置列宽

roc*_*man 7 restructuredtext python-sphinx

我在 rST 中有以下网格表。我想控制HTML 输出的列宽,使其Field type占据表格宽度的 20%、Description30% 和Example50%。

+-------------+-----------------+-----------------------+
|Field type   |Description      |Example                |
+-------------+-----------------+-----------------------+
Run Code Online (Sandbox Code Playgroud)

..tablularcolumns指令没有影响,..table和 的组合也没有影响:width:。例如,以下内容没有影响。

.. tabularcolumns:: |p{2cm}|p{3cm}|p{5cm}|
Run Code Online (Sandbox Code Playgroud)

以下 SO 链接的答案不起作用。

如何修复 reStructuredText 表中的列宽?

任何推荐都会受到彻底的祝福。

Ste*_*rcy 8

两个选择。

\n\n

使用表格widths选项。

\n\n
.. table:: This is my table\n    :widths: 20 30 50\n\n    +-------------+-----------------+-----------------------+\n    |Field type   |Description      |Example                |\n    +-------------+-----------------+-----------------------+\n
Run Code Online (Sandbox Code Playgroud)\n\n

修改主题的 CSS 并使用:nth-childCSS 伪选择器。

\n\n
td:nth-child(1) {\n    width: 20%;\n}\ntd:nth-child(2) {\n    width: 30%;\n}\ntd:nth-child(3) {\n    width: 50%;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

第一个选项的输出如下:

\n\n
<table border="1" class="colwidths-given docutils" id="id1">\n<caption><span class="caption-text">This is my table</span><a class="headerlink" href="#id1" title="Permalink to this table">\xc2\xb6</a></caption>\n<colgroup>\n<col width="20%">\n<col width="30%">\n<col width="50%">\n</colgroup>\n<tbody valign="top">\n<tr class="row-odd"><td>Field type</td>\n<td>Description</td>\n<td>Example</td>\n</tr>\n</tbody>\n</table>\n
Run Code Online (Sandbox Code Playgroud)\n