【问题标题】:How to include image files in Sphinx + Latex Pdf files如何在 Sphinx + Latex Pdf 文件中包含图像文件
【发布时间】:2020-12-06 15:48:50
【问题描述】:

我正在使用 Sphinx 文档工具和 LaTeX 来生成 pdf 文件。

尝试在 pdf 文件中包含图像时,我在编译过程中遇到以下错误:

LaTeX Warning: File `{img_file}.jpg' not found on input line 920.


! Package pdftex.def Error: File `"""{img_file}".jpg' not found: using dra
ft setting.

See the pdftex.def package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.920 ...phinxincludegraphics{{img_file}.jpg}
                                                  }
? 

这些是一些关键的环境变量:

A.在我的conf.py中,我将绝对路径定义为:sys.path.insert(0, os.path.abspath('..'))

B.这是目录树:

docs/           # (Sphinx) Documentation folder 
    _build/
    _static/
        css/
        images/
           abc.jpg
        img_file.jpg          <= This image file is in the root ("_static") directory
    _templates/
    conf.py
    index.rst
    modules/                  <= Folder containing my source (.rst) files
        execute/
            file_with_image.rst           <= I am trying to put the image in this source file
            some_other_file.rst

C.为了包含我正在使用以下内容的图像。

.. image:: /_static/img_file.*

D.我已将 graphix 包含在 conf.pypreamble 中。

为什么会出现此错误?错误消息中那么多“””引号的来源是什么?

编辑

在 Tex 编辑器中打开 *.tex 文件时,图像插入部分的代码片段显示如下:

\subsection{Add Image}
\label{\detokenize{modules/aud_execution/add_image_with_image_directive:add-image}}\label{\detokenize{modules/aud_execution/add_image_with_image_directive::doc}}
\sphinxstylestrong{Adding an image}

Example of image usage using “image” directive:

\noindent{\hspace*{\fill}\sphinxincludegraphics{{img_file}.jpg}} % ***

使用Quick Build,编辑器会抛出如下错误:

!包 pdftex.def 错误:文件 `"""{img_file}".jpg' 不是 发现:...

但是,当图像文件名中的大括号 {{img_file}.jpg} 被删除并更改为(标记为 *** 的行):

\noindent{\hspace*{\fill}\sphinxincludegraphics{img_file.jpg}}%***

文档编译成功,可以在pdf文件中看到图片。

如果保存.tex文件,_build/latex/文件夹中的pdf文件会显示图片。

那么为什么在终端的make latexpdf 期间插入了额外的括号?

【问题讨论】:

  • 我发现了一些东西possibly related in the Sphinx docs。这超出了我的深度。除了 Sphinx 文档,您是否尝试过搜索 TeX community 或 Sphinx issue tracker?
  • 我无法重现这一点,但我可以说sys.path.insert(...) 对图像没有影响。该设置仅在使用 autodoc 时适用。
  • @StevePiercy - 已经检查了您指出的部分,但是正如文档本身所说 源文件中引用的图像文件(例如通过 .. image::) 会自动复制。 而且,正如文档所说,引用的图像文件被复制到 _build/latex 文件夹,这(我认为)意味着 LaTeX 正在访问 中引用的图像文件b>文件 file_with_image.rst。 PS。我已经检查了两个(其他)来源,但没有提出与此问题相关的任何内容。在Github 上发布问题。
  • @mzjn - 没错。我用过sys.path.append(os.path.abspath('../&lt;proj_folder&gt;/docs')),结果是一样的。
  • @StevePiercy - 我创建了一个新的 (Sphinx) 项目,看看是否像我之前所做的那样将它从我的 Django 应用程序中分离出来,会改变这种情况。即使这样,它也无法访问图像文件。我在某处读到(在Tex 上,可能是),在 conf.py 中调用\usepackage 的顺序也可能有一些影响。但即使将graphicx 作为最后一个被调用的,也无济于事。有什么想法吗?

标签: python-sphinx pdflatex pdftex


【解决方案1】:

不要将您的图片放在_static 下。它是专用文件夹,不用于图像。例如。创建modules/execute/img/,将图像移到那里,然后.. image:: img/my-image.png。

【讨论】:

  • _static 文件夹用于存放您的图像,但文件要复制到输出中
  • 感谢您的回复。但是,使用 .. image:: img/my-image.png 会引发 错误 WARNING: a suitable image for html builder not found: img/my-image.png。使用 .. image:: modules/execute/img/my-image.png 正确编译 HTMl 文档并显示图像。但是,和以前一样,使用这两种方法,生成的 pdf 文件根本无法显示图像。 PS我不知何故无法获得您的评论静态文件夹是为您的图像,但文件要复制到输出。如果你能澄清一下。
  • 我的错误。应该是“_static 文件夹不适合您的图像”。
  • 警告“找不到适合 html 构建器的图像”很奇怪。 LaTeX builder 支持 PDF、PNG 和 JPG 格式的图像。见sphinx-doc.org/en/master/usage/builders/…
  • 但是当我使用.. image:: modules/execute/img/my-image.png(标记图像的目录路径)时,没有警告并且图像按预期显示。 我错过了什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多