Consider the following MWE: I have
- a LyX file
/home/user/latex_files/main.lyx
, where I have used\graphicspath{{./img/}}
to set the (relative) path for all my images that I want to include in my file - a TeX
/home/user/latex_files/test.tex
- and an image file in
/home/user/latex_files/img/a.jpg
.
My problem is that LyX cannot compile my file to a pdf as I get a LaTeX Error: File `a' not found.
Yet when I take the code from LyX and paste it without any change into a tex file and compile that with Texmaker, it compiles without any problems. What is going on here?
I also tried using absolute paths in \graphicspath
: Same result! tex file compiles, lyx file doesn't, giving the same error message.
If I give up on using \graphicspath
and specify the path in \includegraphics
both the tex file and lyx file compile perfectly, as expected; so the problem seems to lie with how LyX deals with \graphicspath
.
This is frustrating, because I need to set the folder of my image files manually, since I have a lot of them. If I would set the folder of the image file separately in each \includegraphics
command, and for some reason I need to change my image folder name, then it would be a nightmare if I have to go through the whole code to change every the name in everysingle occurence \includegraphics
. I'm sure other users also would like such an approach to work very much, as it simplifies keeping the code up-to-date as the folder structure changes.
I have started to suspect that this behavior might be a longstanding bug, because in this 8 year old question essentially the same problem was mentioned. His workaround was to use absolute paths, which didn't work for me.
EDIT: This is a bug https://www.lyx.org/trac/ticket/8895 but it has been closed. I have added further tickets, in case a developer has mercy and will fix some or all of the issues regarding how LyX handles this.
The worst thing is that LyX is not gracefully failing: If someone imports a LaTeX file or decides to use \graphicspath
there is no alert coming from LyX that it cannot handle this command.
Here's the code for the LyX file: I have set everything in LyX to "Class Default" and opened the Code Preview pane, selected "Complete Source" and copied it from there. This same source I have then copied into text.tex
, which compiles without problems.
% Preview source code
%% LyX 2.3.4.2 created this file. For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[latin9]{inputenc}
\usepackage{geometry}
\geometry{verbose}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\newlength{\lyxlabelwidth} % auxiliary length
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{graphicx}
\graphicspath{{./img/}}
\makeatother
\usepackage{babel}
\begin{document}
asd
\includegraphics{a} % This needs to be added using "evil red code", because
% adding it using Insert -> Graphics insert the command with weird, additional parameters.
\end{document}
BTW, I'm using Ubuntu 20.04.3 LTS with LyX 2.3.4.2 (which, for my distro, is the latest one).
LaTeX Error: File not found
. :X\graphicspath
and then I spent hours trying to figure out why the import didn't work. A popup alerting me of these problems with\graphicspath
would have been most helpful.