3

I am using Sphinx Documentation tool alongwith LaTeX to generate pdf files.

While trying to include images in the pdf file, I am coming up with the following error during compile:

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}
                                                  }
? 

These are some key environment variables:

A. In my conf.py, I have defined absolute path as: sys.path.insert(0, os.path.abspath('..'))

B. This is the directory tree:

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. For including the image I am using the following.

.. image:: /_static/img_file.*

D. I have included graphix in conf.py preamble.

Why am I getting this error? What is the source of so many """ quotes in the error message?

Edit

On opening the *.tex file in Tex editor, this is what the code fragment from the image insertion section shows:

\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}} % ***

Using the Quick Build, the editor throws error as under:

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

However, when the curly brackets from the image file name, {{img_file}.jpg} are removed and changed it to (line marked ***):

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

the document successfully compiles and the image can be seen in the pdf file.

If the .tex file is saved, the pdf file in the _build/latex/ folder shows the image.

So why are the extra brackets being inserted during make latexpdf from the terminal?

shaan
  • 351
  • 2
  • 15
  • 1
    I found something [possibly related in the Sphinx docs](https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-latex_additional_files). This is out of my depth. Aside from Sphinx docs, have you tried searching the [TeX community](https://tex.stackexchange.com/) or the [Sphinx issue tracker](https://github.com/sphinx-doc/sphinx/issues)? – Steve Piercy Aug 17 '20 at 21:16
  • I cannot reproduce this, but I can say that `sys.path.insert(...)` has no effect on images. That setting only applies when using autodoc. – mzjn Aug 18 '20 at 07:31
  • @StevePiercy - Had already checked the section you pointed out however as the doc itself says **Image files that are referenced in source files (e.g. via .. image::) are copied automatically.** And also, as the doc says, the **referenced image file is** getting copied to the `_build/latex` folder, which (I think) means that LaTeX is accessing the image file being referenced in the **file** `file_with_image.rst`. _PS_. I have checked both the (other) sources, but not coming up with anything related to this **issue**. Posted issue on [Github](https://github.com/sphinx-doc/sphinx/issues/8135). – shaan Aug 18 '20 at 07:37
  • @mzjn - That's right. I have used `sys.path.append(os.path.abspath('..//docs'))` and the result is the same. – shaan Aug 18 '20 at 07:47
  • @StevePiercy - I created a new (Sphinx) project to see if divorcing it from my Django app as I was doing earlier, would change the situation. Even then it failed to access the image file. Somewhere I read (on [Tex](https://tex.stackexchange.com/), may be) that the order the `\usepackage`'s are called in the **conf.py** also may have some bearing. But even keeping `graphicx` as the last one to be called, hasn't helped. Any ideas? – shaan Aug 20 '20 at 03:14
  • You may also want to look into using rinoh for generating PDFs with sphinx https://github.com/maltfield/rtd-github-pages/blob/347ec2ed9522d3b5ec6e3e89f0d53bdcee86788b/docs/buildDocs.sh#L74-L77 – Michael Altfield Aug 20 '20 at 10:12

1 Answers1

0

Don't place your images under _static. It is special-purpose folder, not for images. E.g. create modules/execute/img/, move image there, and .. image:: img/my-image.png.

Matt Warrick
  • 1,385
  • 1
  • 13
  • 22
  • _static folder is for your images, but files to be copied to the output – Matt Warrick Aug 21 '20 at 07:16
  • Thanks for your reply. However, using `.. image:: img/my-image.png` raises **error** `WARNING: a suitable image for html builder not found: img/my-image.png`. Using `.. image:: modules/execute/img/my-image.png` correctly compiles the HTMl docs and images are displayed. But, as before, using both the methods, the **generated pdf file fails to display images at all**. _PS_ I somehow could not get your comment **static folder is for your images, but files to be copied to the output**. If you could clarify. – shaan Aug 21 '20 at 10:36
  • My mistake. Should be "_static folder is NOT for your images". – Matt Warrick Aug 21 '20 at 11:00
  • The warning "a suitable image for html builder not found" is strange. LaTeX builder support images in PDF, PNG and JPG. See https://www.sphinx-doc.org/en/master/usage/builders/index.html#sphinx.builders.latex.LaTeXBuilder.supported_image_types – Matt Warrick Aug 21 '20 at 11:02
  • But when I am using `.. image:: modules/execute/img/my-image.png` (**mark the directory path to the image**) there is no **WARNING** and images are displayed as expected. **Am I missing something here?** – shaan Aug 21 '20 at 11:16
  • I see! So the problem is path to the image. You must know whether `img/my-image.png` or `modules/execute/img/my-image.png`. It depends on the .rst file where `image::` is. If image is at `modules/execute/img/my-image.png`, from `modules/execute/foo.rst` file, relative path `img/my-image.png` should work. – Matt Warrick Aug 21 '20 at 12:31
  • I had tried to include as much info as possible in my original question (prior to adding an **Edit**). If you could browse thru' s.no. **A to D**. (Ref _D_, in the preamble I have added `graphicx`). I would be happy to provide any other info if it helps. – shaan Aug 21 '20 at 13:58
  • Even if _static may be considered "special-purpose", there are no problems with having images in that folder. It works for both HTML and LaTeX/PDF output. I cannot reproduce the problem in the question. – mzjn Aug 22 '20 at 06:24