Select Sphinx 中的图像扩展

Select image extension in Sphinx

我目前正在使用 Sphinx 为我的 Python 包编写文档。我有我想使用的矢量图像,但 HTML 文档只接受 SVG,而 PDF 文档只适用于 PDF 文件。没问题,我有两个版本的图片。

我正在尝试使用 only 指令来选择要包含在我的文档的不同版本中的图像,如下所示:

.. only:: latex

    .. image:: ../docs/alpha-strat.pdf

.. only:: html

    .. image:: ../docs/alpha-strat.svg

然后我用 make clean pdflatex html 编译它,但 Sphinx 在两个版本的文档中都使用 PDF 图像。有什么想法吗?

您可以通过replacing the image filename extension with an asterisk让后端自动使用支持格式的图片:

.. image:: ../docs/alpha-strat.*

来自上面链接的 Sphinx 文档:

Sphinx then searches for all images matching the provided pattern and determines their type. Each builder then chooses the best image out of these candidates.