6

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.

  • Does `\graphicspath{{chap1/images/}{chap2/images/}....{chapN/images}}' help? Also, what about the answer given here: tex.stackexchange.com/questions/172320/… – jak123 Jan 26 '16 at 11:41
  • To me, this is not better than using a separate command before each call of chapN.tex. But, the provided link worked almost perfectly. Almost because -- images are placed chap1/images/f1.png thus, I would use \includegraphics[...]{images/f1}. That is also great. However, if possible, I would love to use it something like \includegraphics[...]{f1} only. – novice Jan 26 '16 at 12:41
2

If you add \providecommand{\main}{..} in each of your subfiles before documentclass and \providecommand{\main}{.} in the same position in the main file, you can now replace every relative paths with, for example, \main/images/f1.png. The commands define \main as .. (which means folder above) in the subfiles but as . (same folder) in the main document. Providecommand defines a command only if not already defined, so when you compile main.tex the line \providecommand{\main}{..} in the subfiles is basically ignored. This doesn't happen in the subfiles when they are compiled independently.

This will also allow you to place all your images in the images folder, which should be a subfolder of the main folder. Your directory structure could now become:

/Diss/main.tex
/Diss/images/
/Diss/images/f1.png
/Diss/images/f2.png
...
/Diss/chap1/
/Diss/chap1/chap1.tex
...

MWE. main.tex

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

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

\usepackage{subfiles}

\usepackage{blindtext}

\begin{document}

\subfile{\main/chap1/chap1}

\end{document}

chap1.tex

\providecommand{\main}{..}
\documentclass[\main/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 is a strikingly elegant solution and helped me solve a problem with complex structures that used the packages catchfilebetweentags, standalone, and listings shared between many projects. – anthsts Dec 9 '18 at 20:09
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.