调整 pdf 输出的 table 宽度以完全适合线宽

Adjust table widths for pdf output to perfectly fit the line width

我的公司使用 Sphinx 来创建我们的用户手册,我们正在广泛使用 table 的不同品种。在许多情况下,需要手动调整 table 的列宽以生成高质量的 pdf 输出。

为此我尝试使用:

.. tabularcolumns:: |p{0.2\linewidth}|p{0.8\linewidth}|

+------+--------+
| A    | B      |
+------+--------+

我的意图是,table 应该完全适合周围的文字, 但有一点重叠。看似百分比不占内框占用的space。

我怎样才能正确计算出所需的数量,使盒子正好适合 space?

对于 Sphinx 1.6 或更高版本,您可以使用

.. tabularcolumns:: |\Y{0.2}|\Y{0.8}|

(是\Y,不是Y。)见http://www.sphinx-doc.org/en/stable/markup/misc.html?highlight=tabularcolumns#directive-tabularcolumns

对于 Sphinx 1.5 或更高版本,您可以使用 |\X{2}{10}|\X{8}{10}|

Sphinx < 1.5 你需要一些特殊的 LaTeX,你可以从这段代码中提取出来

\newcolumntype{\X}[2]{p{\dimexpr
      (\linewidth-\arrayrulewidth)*#1/#2-2\tabcolsep-\arrayrulewidth\relax}}
\newcolumntype{\Y}[1]{p{\dimexpr
      #1\dimexpr\linewidth-\arrayrulewidth\relax-2\tabcolsep-\arrayrulewidth\relax}}

来自当前 sphinx.sty,它解释了 \X\Y 是如何定义的。