1

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).

8
  • \graphicspath is not supported by lyx and according to this ticket it won't be fixed lyx.org/trac/ticket/8895 Commented Feb 25, 2022 at 10:52
  • 1
    @UlrikeFischer Oh. I'm really sad to hear that. :( That is definitely a dent in terms of LyX userfriendliness! I can't image what determined them to make this decision? Or is there any other way to easily update the location of image files if I change their directory? (With "easily" I mean more easily than using a cumbersome search & replace, as mentioned in the bugtracker you linked, as the only alternative.) I've been using LyX for years now and this is my biggest disappointment so far.
    – l7ll7
    Commented Feb 25, 2022 at 11:03
  • Furthermore, LyX should at least provide a meaningful error message if people try to use this command, so that it gracefully fails, instead of simply returning a cryptic LaTeX Error: File not found. :X
    – l7ll7
    Commented Feb 25, 2022 at 11:05
  • Well ask them. You now know where the bug tracker is. (I don't use lyx.) Commented Feb 25, 2022 at 11:08
  • 1
    @scottkosty Is there no push to provide a patch to make relative paths work? I'm surprised not more people have asked this. At least a pop-up would be helpful for users - my case actually arose when I imported a latex file that used \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.
    – l7ll7
    Commented Feb 25, 2022 at 15:49

1 Answer 1

1

It is necessary to understand that:

  1. Lyx does not compile the files of the working directory, but a copy of the used files in a temporal directory as /tmp/lyx_tmpdir.ezqVeYKSCOmQ or so ...

  2. On the other hand, LyX do not know and have not any control in what you put in the LaTeX preamble manually (the part added with DocumentSettingsLaTeX preamble).

  3. In the dialog to select the image (InsertGraphics...) you must set the path. Otherwise, as the image is not found, Lyx insert the option draft to the image, so you must include the image with a ERT (evil red text) as you discover ...but first also \usepackage{graphicx} in the manual LaTeX preamble, because for LyX there are no images in the document, so apparently there are no reasons to load `graphicx.

  4. Moreover, because this way for LyX there are no images, they are copied to the temporal directory, so view the PDF (Ctrl+R) still fail.

  5. What will work, however, is export to LaTeX (FileExportLaTeX (pdflatex) to the working directory and then compile it externally, because in that place the path is correct.

Said that, for people using WYSIWYM editor, this goings-on is a pain. IMHO, is just better forget \graphicpath, enjoy the image previews in the editor, and if the path to the images should be changed someday, a search and replace of the obsolete path in the .lyx file is faster that edit it with LyX to modify the preamble.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .