1

Hi I tried to use the import command from the import package to not conflict with the relative paths in the imported file. However, this doesn't seem to work with \graphicspaths.

Here the structure of my directories

folder/
   a5version/
       a5version.tex
   source/
       master.tex
       chapters/
          chapter1.tex
       images/
          buw.pdf

Then the a5version.tex looks like

\documentclass{article}
\usepackage{import}
\import{./../source/}{master.tex}

the master.tex looks like

\usepackage[utf8]{inputenc}
\usepackage{graphics}
\graphicspath{{./images/}}
 
\begin{document}
\chapter{chapter 1}
\input{./chapters/chapter1.tex}
\end{document}

and chapter1.tex looks like

This is the first chapter. Here is a graphic:
\includegraphics{buw.pdf}

When I try to compile a5version.tex I get the following error:

ERROR: Package pdftex.def Error: File `buw.pdf' not found: using draft setting.

Is there a way to fix this? I mean clearly I could just don't use \graphicspath and use includegraphics{./images/buw.pdf}, but this is a little bit inconvenient.

4
  • 2
    personally I wouldn't use import just make source your current dorectory and do pdflatex ../a5version/a5version then all filepaths are naturally relative to the master document. But if you want to use it I would assume addding \graphicspath{{images/}{../source/images/}} probably works. Oct 5, 2021 at 11:41
  • i don't understand how it helps to change the location of a5version, because if I compile a5version.tex, then everything has to be relative to a5version.tex. However, the second part combined with the answer of gernot does what I want. Oct 5, 2021 at 13:23
  • no paths are not relative to a file they are relative to the working directory of the tex process, so if you are in the directory containing master when you start pdftex then all paths are relative to that location even if the document specified on the commandline is elsewhere Oct 5, 2021 at 13:26
  • @DavidCarlisle okay I didn't know that, but I usually compile in a latex environment. Oct 5, 2021 at 13:55

3 Answers 3

1

Replace

\graphicspath{{./images/}}

by

\makeatletter
\graphicspath{{\import@path/images/}}
\makeatother

You have to make sure that master.tex is always \imported, not just \input, as otherwise \import@path will not be defined.

6
  • combining this with the comment to my question also allows this input the master file in the same directory. Oct 5, 2021 at 13:21
  • @NathanaelSkrepek Why do you have to change the graphics path? Just use \import{.}{master.tex}
    – gernot
    Oct 5, 2021 at 13:27
  • thats true, but if for example i want to add in master file if\IsAfive0 \documentclass{report}\fi such that the master file can be compiled own its own, then it is relevant Oct 5, 2021 at 13:52
  • 1
    @NathanaelSkrepek In this case you could add \makeatletter\def\import@path{.}\makeatother between \documentclass{} and \fi. But it is debatable whether this is simpler.
    – gernot
    Oct 5, 2021 at 13:54
  • ah thanks. this is also possible. I did it the other way around. I changed the graphicspath only if IsAfive equals 1. Oct 5, 2021 at 13:57
1

The OP mentions in the comments that the original file should also be compilable on its own. In this case, the subfiles package may help to hide a bit of the machinery.

% a5version/a5version.tex
\PassOptionsToClass{a5paper}{book}
\documentclass[../source/master]{subfiles} % get preamble from ../source/masters.tex
% ... additional settings for a5version.tex ...
\begin{document}
\subfile{../source/master}% import contents of document, skipping its preamble
\end{document}

% source/master.tex
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{subfiles}
% subfiles package has to be loaded before \graphicspath
\graphicspath{{images/}}% graphics path relative to this file
\begin{document}
\chapter{chapter 1}
\input{./chapters/chapter1.tex}% input path relative to this file
\end{document}
8
  • I think this is really what I have been looking for. Oct 5, 2021 at 15:05
  • I again get the error message: ERROR: Package pdftex.def Error: File `buw.pdf' not found: using draft setting. Oct 5, 2021 at 15:15
  • @NathanaelSkrepek Do you have the \graphicspath statement after \usepackage{subfiles}?
    – gernot
    Oct 5, 2021 at 15:36
  • @NathanaelSkrepek Otherwise, check whether you have recent versions of LaTeX and the subfiles package. Both should be dated October 2020 or newer.
    – gernot
    Oct 5, 2021 at 15:39
  • ah i found the error. interestingly for \graphicspath neither the package graphics nor graphicx is necessary, but for this solution it is. Thanks a lot for your support! Oct 5, 2021 at 15:49
0

In yet another comment, Nathanael asks for a solution in the case that the document should be typeset with another document class, but remaining unchanged otherwise. If the two versions are in the same folder, then

% a5version.tex
\documentclass[a5paper]{scrbook}
\newcommand\skipdocumentclass[2][]{\let\documentclass\origdocumentclass}
\let\origdocumentclass\documentclass
\let\documentclass\skipdocumentclass
% ... additional definitions for a5version
\input{master}

should do the trick. If the main document is somewhere else and inputs/includes/imports other files itself, you will need something like

% a5version/a5version.tex
\documentclass[a5paper]{scrbook}
\RequirePackage{import}
\newcommand\skipdocumentclass[2][]{\let\documentclass\origdocumentclass}
\let\origdocumentclass\documentclass
\let\documentclass\skipdocumentclass
% ... additional definitions for a5version
\import{../source}{master}

If the main document uses \graphicspath and is not in the same folder as its clones, load the package subfiles in the preamble before \graphicspath.

% source/master.tex
\documentclass...
...
\usepackage{subfiles}
...
\graphicspath{..}

The subfiles package redefines \graphicspath such that it add \import@path, which ensures that the path specifications are interpreted relative to the folder of master.tex, not to the one of the file in the latex command.

You must log in to answer this question.

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