[Tex/LaTex] path of figures in different directories with subfile latex

graphicspdftexsubfiles

This is the structure of my directories –

  • Main tex file /Diss/main.tex
  • chapter tex files Diss/chap1/chap1.tex, Diss/chap2/chap2.tex etc
  • Images Diss/chap1/images/f1.png, Diss/chap2/images/f2.png etc

I would like to compile main file as well as each chapter standalone. So, I find subfile package useful. for e.g.

main.tex

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}

%here is the path
\graphicspath{{chap1/images/}{images/}}

\usepackage{subfiles}

\usepackage{blindtext}

\begin{document}

\subfile{chap1/chap1}

\end{document}

chap1.tex

\documentclass[../main.tex]{subfiles}

\begin{document}

\begin{figure}[bh]
\centering
\includegraphics[width=4cm]{f1}

\label{fig:img1}
\caption{ShareLaTeX learn logo}
\end{figure}

Hello, here is some text...
\end{document}

This works nicely. So far so good.
Now, I have several chapters, and most likely, using \graphicspath{{chap*/images/}{images/}} before each call of chapter will work (* is the chapter number). But, I don't really like it because, I must add it several times. Is there a better way to manage this type of structure.

P.S. – I invested a lot of time on it, but could not find a satisfactory answer, probably my search direction is wrong.

Best Answer

I approach this type of problem in this way. I have a command defined in the preamble that (re)sets the graphics path ...

\newcommand*{\resetfigpath}[1]{
    \graphicspath{{#1/figures/}}
}

I call the command at the start of each chapter ...

\resetfigpath{chapter01}

My understanding is, one should keep the list of folders in graphics path as short as possible to minimize the time to parse through the search paths. Also, I have had no issues (re)setting the graphics path at intermediate points within the document body.

I am curious why this approach is not an obvious or otherwise viable one.