[Tex/LaTex] Can’t indirectly include images in LyX

graphicslyxpdftex

In a folder example/ there are subdirectories example/chapter1/, example/chapter2/ etc.

Some images occur in more than one chapter, hence I made a subdirectory example/images/ where I put the images, let's call it example/images/image.pdf.

In the base directory there is example/images.sty providing a command for including the image (and does some more stuff I stripped for this MWE):

\usepackage{graphicx}
\graphicspath{{../images/}}
\def\myimage{\includegraphics{image.pdf}}

Now some file in a subdirectory, let's say example/chapter1/main.tex, uses that command like this:

\documentclass{article}
\usepackage{../images}
\begin{document}
\myimage
\end{document}

When compiling with pdflatex on the command line this works. But when using LyX this doesn't work: import example/chapter1/main.tex into LyX (so example/chapter1/main.lyx is created) and try creating pdflatex via LyX and it claims File image.pdf not found. (And when using \includegraphics directly in main.tex instead via images.sty it works, too.)

Best Answer

Problem was that as far as LyX is concerned .sty files are raw LaTeX, hence only the file gets copied to the temporary directory where LyX does the compilation but not the dependencies referenced in that file - these get resolved only for .lyx files.

I went with using an absolute path \graphicspath{{/home/username/latex/example/images/}}. That's going to give me trouble only when I move it to a new directory or computer - and then I would have only to change the directory reference.