使用Sphinx文档如何为HTML构建指定png图像格式,为Latex/pdf构建指定pdf图像格式?

使用Sphinx文档如何为HTML构建指定png图像格式,为Latex/pdf构建指定pdf图像格式?,latex,pdf-generation,documentation-generation,python-sphinx,Latex,Pdf Generation,Documentation Generation,Python Sphinx,使用sphinx文档生成器,我尝试将.png图像用于文档的HTML构建,然后我希望将.svg图像用于PDF/LATEx构建 有人知道如何将部分“标记”为“仅HTML构建”和“仅Latex构建”吗 干杯看看这些选项: 图像文件名通配符: .. image:: gnu.* 例如,如果给定了文件名gnu.*,并且源代码树中存在两个文件gnu.pdf和gnu.png,LaTeX生成器将选择前者,而HTML生成器将选择后者 该指令: .. only:: latex This appears o

使用sphinx文档生成器,我尝试将.png图像用于文档的HTML构建,然后我希望将.svg图像用于PDF/LATEx构建

有人知道如何将部分“标记”为“仅HTML构建”和“仅Latex构建”吗


干杯

看看这些选项:

  • 图像文件名通配符:

    .. image:: gnu.* 
    
    例如,如果给定了文件名gnu.*,并且源代码树中存在两个文件gnu.pdf和gnu.png,LaTeX生成器将选择前者,而HTML生成器将选择后者

  • 该指令:

    .. only:: latex
    
       This appears only in LaTeX output.
    
    .. only:: html
    
       This appears only in HTML output. 
    

  • 可以使用makefile自动生成适当的输出格式

    还提供了一个教程,演示了类似的过程

  • 使用.rst源中的图像

    .. image:: my_image.*
    
  • 使用Inkscape在构建时将源图像转换为PDF和PNG。通过将以下代码添加到Makefile,可以在生成时自动执行此操作:

    SOURCEDIR     = source
    #IMAGEDIRS can be a list of directories that contain SVG files and are relative to the SOURCEDIR
    IMAGEDIRS      = _images
    
    # SVG to PDF conversion
    SVG2PDF       = inkscape
    SVG2PDF_FLAGS = -C
    
    # SVG to PNG conversion
    SVG2PNG       = inkscape
    SVG2PNG_FLAGS = -C -d=90 --export-background-opacity=\#00
    
    # Pattern rule for converting SVG to PDF
    %.pdf : %.svg
        $(SVG2PDF) $(SVG2PDF_FLAGS) -f $< -A $@
    
    # Pattern rule for converting SVG to PNG
    %.png : %.svg
        $(SVG2PNG) $(SVG2PNG_FLAGS) -f $< -e $@
    
    # Build a list of SVG files to convert to PDFs
    PDFs := $(foreach dir, $(IMAGEDIRS), $(patsubst %.svg,%.pdf,$(wildcard $(SOURCEDIR)/$(dir)/*.svg)))
    
    # Build a list of SVG files to convert to PNGs
    PNGs := $(foreach dir, $(IMAGEDIRS), $(patsubst %.svg,%.png,$(wildcard $(SOURCEDIR)/$(dir)/*.svg)))
    
    # Make a rule to build the PDFs
    images-pdf: $(PDFs) 
    
    # Make a rule to build the PNGs
    images-png: $(PNGs) 
    
    # Make a rule to build the images
    images: images-pdf images-png
    
    clean-pdf:
        -rm $(PDFs)
    
    clean-png:
        -rm $(PNGs)
    
    clean-images: clean-pdf clean-png
    
    现在,您可以通过键入
    make images
    来构建图像,并使用
    make clean images
    来清理图像。使用
    make html
    make latex
    make latexpdf
    将自动确保您的图像是最新的

  • 一个问题是Sphinx默认在HTML输出中优先使用SVG而不是PNG。您可以通过覆盖
    conf.py
    文件中的preferece来修复此问题

    导入后,在
    conf.py
    文件顶部附近添加以下行

    # Redefine supported_image_types for the HTML builder
    from sphinx.builders.html import StandaloneHTMLBuilder
    StandaloneHTMLBuilder.supported_image_types = ['image/png', 'image/svg+xml', 
                                           'image/gif', 'image/jpeg']
    

  • 谢谢@mzjn。。只::正是我想要的:)我记得在文档中看到它,并认为有一天我会需要它,然后我很快就忘了,再也找不到了!干杯完美的这适用于让HTML构建器使用SVG,让LaTeX使用PDF作为图像。
    # Redefine supported_image_types for the HTML builder
    from sphinx.builders.html import StandaloneHTMLBuilder
    StandaloneHTMLBuilder.supported_image_types = ['image/png', 'image/svg+xml', 
                                           'image/gif', 'image/jpeg']