gsalzer / subfiles

class and package for multi-file projects in LaTeX
LaTeX Project Public License v1.3c
25 stars 1 forks source link

Can't include graphics from a folder relative to subfile #19

Closed mje-nz closed 3 years ago

mje-nz commented 3 years ago

I've just updated to subfiles 2.2. Previously I had my own method for setting \graphicspath, which clashes with subfile's new \graphicspath handling. Here is an example showing my project structure:

main.tex
chapters/
    chapter1/
        chapter1.tex
        img/
            image.jpg
    ...
% main.tex
\documentclass{article}
\usepackage{subfiles}
\usepackage{graphicx}
\graphicspath{{img/}}

\begin{document}
\subfile{chapters/chapter1/chapter1}
% ...
\end{document}
% chapter1.tex
\documentclass[../../main]{subfiles}
\begin{document}
\includegraphics{image}
\end{document}

With this example, the image is not found.

I'm can't tell from the documentation whether this is supposed to work or not: does subfiles make it so that graphics paths are relative to the top-level document, or relative to each subfile? I'm starting to think it makes them relative to the top-level document and also adds the current directory for each subfile. If that's the case, then that's a bit surprising and it would be nice if it was explicit in the documentation.

gsalzer commented 3 years ago

Thanks for your feedback. The documentation could probably be clearer, I will add some more examples in the next version.

The general philosophy is that file paths are defined relative to the file they are specified in, and that the subfiles package should be loaded as the last package of the preamble. The following two scenarios should work.

Graphics path relative to main file Use this approach if other parts of the document, unrelated to chapter1.tex, are supposed to access the images in chapters/chapter1/img.

% main.tex
\documentclass{article}
\usepackage{graphicx}
\usepackage{subfiles}
\graphicspath{{chapters/chapter1/img/}}

Graphics path relative to subfile Use this approach if chapters/chapter1/img contains exclusively images used by chapter1.tex. Note that \graphicspath has to appear after \begin{document} in the subfile, as otherwise the statement would not be executed if the file is loaded from the main file.

% main.tex
\documentclass{article}
\usepackage{graphicx}
\usepackage{subfiles}

% chapter1.tex
\documentclass[../../main]{subfiles}
\begin{document}
\graphicspath{{img/}}
Dolfost commented 3 weeks ago

Have something changed? Is there neater solution now?

gsalzer commented 3 weeks ago

Have something changed? Is there neater solution now?

Can you give some more details on your problem? The issue described above is not really one, just a clarification on how to use the package properly. The currently implemented strategy seems to follow a natural and consistent philosophy, so based on this issue, there are no plans to change anything. But if your issue is actually different and maybe actually a bug, things may change. Maybe open a new issue if the situation above does not exactly match yours.

Dolfost commented 3 weeks ago

Have something changed? Is there neater solution now?

Can you give some more details on your problem? The issue described above is not really one, just a clarification on how to use the package properly. The currently implemented strategy seems to follow a natural and consistent philosophy, so based on this issue, there are no plans to change anything. But if your issue is actually different and maybe actually a bug, things may change. Maybe open a new issue if the situation above does not exactly match yours.

So, I can explain. I want to do as You wrote in "Graphics path relative to subfile", but isntead of putting \graphicspath{{img/}} at beginning of each subfile I want to put the \graphicspath{{\subfix{./img/}}} in the preamble of main document to avoid repeating. There is no bugs, so You do not need to worry.

Also, I am interested in what you meant by "Some commands already apply the fix on the fly. At the moment these are the standard LATEX command \bibliography and \graphicspath from the graphics/graphicx package." in the texdoc subfiles at page 3.

gsalzer commented 3 weeks ago

Not sure I understand what you mean, but I'll try to answer nevertheless.

Suppose you have the following structure of files and directories:

main.tex
img/
img/a.jpg
subfiles/
subfiles/sub1.tex

and you want to make img/a.jpg available to main.tex and sub1.tex, then you can do this:

% main.tex
\documentclass{article}
\usepackage{graphicx}
\usepackage{subfiles}
\graphicspath{{img/}}
\begin{document}
\includegraphics{a.jpg}
\end{document}

% subfiles/sub1.tex
\documentclass[../main]{subfiles}
\begin{document}
\includegraphics{a.jpg}
\end{document}

Note that when LaTeXing subfiles/sub1.tex directly, the correct path to a.jpg is not img/a.jpg, but ../img/a.jpg. This modification is what \subfix takes care of. So normally, you would have to write \graphicspath{{\subfix{img/}}} instead of \graphicspath{{img/}} in main.tex. However, for the LaTeX commands \graphicspath and \bibliography this is done automatically, therefore you can (actually, have to) write \graphicspath{{img/}} as in the example above. This is what the docs mean by saying that \subfix is added automatically for \graphicspath and \bibliography.

If my explanation does not apply to your situation or does not answer your question, please describe your setting in a more detailed way, like I did above.

Dolfost commented 3 weeks ago

Not sure I understand what you mean, but I'll try to answer nevertheless.

Suppose you have the following structure of files and directories:

main.tex
img/
img/a.jpg
subfiles/
subfiles/sub1.tex

and you want to make img/a.jpg available to main.tex and sub1.tex, then you can do this:

% main.tex
\documentclass{article}
\\usepackage{graphicx}
\usepackage{subfiles}
\graphicspath{{img/}}
\begin{document}
\includegraphics{a.jpg}
\end{document}

% subfiles/sub1.tex
\documentclass[../main]{subfiles}
\begin{document}
\includegraphics{a.jpg}
\end{document}

Note that when LaTeXing subfiles/sub1.tex directly, the correct path to a.jpg is not img/a.jpg, but ../img/a.jpg. This modification is what \subfix takes care of. So normally, you would have to write \graphicspath{{\subfix{img/}}} instead of \graphicspath{{img/}} in main.tex. However, for the LaTeX commands \graphicspath and \bibliography this is done automatically, therefore you can (actually, have to) write \graphicspath{{img/}} as in the example above. This is what the docs mean by saying that \subfix is added automatically for \graphicspath and \bibliography.

If my explanation does not apply to your situation or does not answer your question, please describe your setting in a more detailed way, like I did above.

Im sorry, I have understood where I were wrong and apply Your explanation. Thank You!