How to include image files in Sphinx + Latex Pdf files

1.8k Views Asked by At

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?

1

There are 1 best solutions below

8
Matt Warrick On

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.