0

As i cant comment on posts yet, i will have to ask this question. Here is the link with the example: (The last answer by markellos is what im using and reffering to)

path of figures in different directories with subfile latex

His case:

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}

So lets say that my main images directory is the 'images', and you call that by using: \includegraphics[width=4cm]{./f1} (at least i do)

But i also have a subfolder within 'images' called 'ABC'.. How to i call from that using \includegraphics ?

I not very good at latex and i mainly copy things off from answers and other templates to mix for my own likings. Im writing my thesis, and i will have several other subfolder containing images (im doing histology). So it would be very convenient to sort them out in different folder from the main 'images' folder.

I tried \includegraphics{../f1), but something tells me that i have to modify this line in the main.tex to include my subfolders:

\graphicspath{{\main/images/}{images/}}

But how?

Br

0

Forgive the family analogy but it may help you grasp how your relatives are structured and how they are referenced

So you are a folder lets call you Thomas and you have a parent lets call them blindhardt you are placed in a system as /blindhardt/thomas but for self reference you can use the alias "."

you have children /blindhardt/thomas/janet and /blindhardt/thomas/john
they may have nicknames such as preface and chap1
to indicate your childs relation to yourself you can say ./preface or ./chap1
for your father you can say .. and for siblings they may be ../older or ../younger
your family tree can now be

/blindhardt/older
           /thomas/
           /thomas/preface
           /thomas/chap1
           /younger

Ok thats the basic theory of your relative paths now to your answer
One part of you Thomas the custodian (of the family bible) is known as main.tex

markellos shows neatly that if that part of you (main.tex) identifies as

\providecommand{\main}{.}

then any ../child/story.tex can refer to you as their provider

\providecommand{\main}{..}   i.e their own parent

also their additional statement

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

confirms they are of the same subgroup (class) as yourself

as a family you agree to keep all your pictorial assets in
/blindhart/somewhere/vault however as custodian you wish to call it

/blindhart/thomas/images

% here is the vault path for you ./images and the next generation {\main\images/}

\graphicspath{{images/}{\main/images/}}

OK nothing new so far, lets say you keep a folder (ABC) in that vault
both you and your children will now both need to have some cross index.

 \graphicspath{{images/}{\main/images/}{images/ABC/}{\main/images/ABC/}}

So now it does not matter if pic1.png or fig1.pdf is in either main/images or /images/ABC
all of your imediate family of files can find it with

\includegraphics[width=4cm]{pic1}

Main.tex (just one line changed)

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

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

\usepackage{subfiles}
\usepackage{blindtext}
\begin{document}
\subfile{\main/chap1/chap1}
\end{document}

chap1.tex (no change, except a wider acceptance)

\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}
\begin{document}
\begin{figure}[bh]
\centering
% f1 can be any accepted image (png pdf etc.) beware if there are two in different locations then the order WILL become important.
% it can be in ./f1.png it may be in ../images/f1.pdf or even ../images/ABC/f1.eps
\includegraphics[width=4cm]{f1}
\label{fig:img1}
\caption{ShareLaTeX learn logo}
\end{figure}
Hello, here is some text...
\end{document}  

Obviously /older and its /children can follow the same pattern but if you all want to access the family crest in /blindhardt/images then you start adding many more nested relative layers and it may be easier to include an absolute address for such master images thus you and your siblings could add

\graphicspath{{c:\familydocs\blindhardt\images/}{images/}{\main/images/}{images/ABC/}{\main/images/ABC/}}  

There are ways to include spaces in the above but for fullest compatibility I suggest we don't include any

All of the above is based on the wording of your question, however every tex file can visit the public library by itself.

There is a default library at your ${TEXMFHOME}/tex/generic/images

You can also nominate a sequence of project collectives via the TEXINPUTS=".:.//:c:\road\to\nowhere: " variable which you could if you know what your doing re-define differently prior to each project run.

NOTE the special token .// which means ALL subdirectories

See Automatically Locate Included Images and Renaming chapter folder and referring to images efficiently

  • 1
    Your stupid-simple analogy did the trick :)! – Thomasblindhardt Mar 3 at 8:40
1

welcome to tex.SE

For a larger project i coded something that might help you a while ago.

The basic idea is to add a hook at the beginning and the end of \input and \include to adapt the graphicspath automatically. Since \subfile uses \input it should work as well.

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

% use parent management with current source file 
\usepackage{subfiles} 
\usepackage{filehook} 
\usepackage[parent]{currfile} 

\makeatletter
% Define a new stack by letting it to \@empty 
\newcommand\newStack[1]{\global\let#1\@empty} 
% add element to stack (top): \addStack{<stack>}{<element>} 
\newcommand{\addStack}[2]{\def\ele@ment{{#2} }\xdef#1{\ele@ment #1}} 
% add element to stack (bottom) \revaddStack{<stack>}{<element>} 
\newcommand{\revaddStack}[2]{\def\ele@ment{{#2} }\xdef#1{#1 \ele@ment}} 
% remove from stack
\long\def\pop@#1 #2\@nil#3{\def\popedStackElement{#1}%
  \def#3{#2}}% remaining list 
% stack name #1 
\newcommand\popStack[1]{\let\popedStackElement\@empty% 
  \ifx #1\@empty\message{LaTeX warning: you are trying to remove an element from the empty stack \noexpand#1 on input line \the\inputlineno}\else\expandafter\pop@#1\@nil#1\fi} 
% whipe stack 
\newcommand\cleanStack[1]{\global\let#1\@empty} 

%%% enhanced graphicspath management 
% idea: everytime \includegraphics is called from a file of a (larger) project the graphics file is searched 
%       in a specific folder relative to that one the actual tex source code resides in. 
%       In addition generally valid paths can be defined. 
% idea taken from https://tex.stackexchange.com/questions/45946/stack-datastructure-using-latex ;
% uservariant and autovariant of graphicspath stack 
\newStack{\GrapPa@user} 
\newStack{\GrapPa@auto} 
% activate stack as graphicspath 
% def hook for entering file: add path and update graphicspath 
\newcommand{\grappa@start@hook}{%
  \ifcurrfiledir\@empty{}{\addStack{\GrapPa@auto}{\currfiledir\graphicsfolder}}\update@grappa} 
% def hook for leaving file: remove path and update graphicspath 
\newcommand{\grappa@end@hook}{%
  \ifcurrfiledir\@empty{}{\popStack{\GrapPa@auto}}\update@grappa}
% activate stack as graphicspath 
\newcommand{\update@grappa}{\xdef\GrapPa@temp{{\GrapPa@user \GrapPa@auto}}\expandafter\graphicspath\GrapPa@temp} 
%
% user space graphicspath stack: 
\newcommand{\newaddgraphicspath}[1]{\addStack{\GrapPa@user}{#1}\update@grappa} 
\newcommand{\removelastgraphicspath}{\popStack{\GrapPa@user}\update@grappa} 
\newcommand{\showgraphicspath}{\message{user: \show\GrapPa@user\space - auto: \show\GrapPa@auto}} 
% define default graphics folder name
\newcommand{\graphicsfolder}{images/}
% now register for linking to filehook 
\AtBeginDocument{%
  \AtBeginOfInputs{\grappa@start@hook}%
  \AtBeginOfIncludes{\grappa@start@hook}%
  \AtEndOfInputs{\grappa@end@hook}%
  \AtEndOfIncludes{\grappa@end@hook}} 
%%% enhanced graphicspath management
\makeatother

\newaddgraphicspath{./\graphicsfolder} 

\begin{document}
\subfile{./chap1/chap1}
\end{document}

With this \inputs and \includes can be nested and the graphic files are loaded in the folder specified by \graphicsfolder relativ to the directory the tex file resides in.

If you have additional global paths you can add them by calling \newaddgraphicspath. However using \removelastgraphicspath is tricky and you should avoid it unless you know what you are doing.

Calling \showgraphicspath gives you the actual path list searched in the logfile and the console.

Compared with this great and simple answer this solution has the feature that if the image is not found at the same level all levels up are searched also - so putting images in the imgage-folder within the first level provides access to all subsequent levels in this branch. Using that you can for example add different markers to different parts and levels without the need to copy imagefiles deeply nested or changing macros.

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.