How to /includegraphics from sub directory of main image folder, using subfiles












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










share|improve this question



























    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










    share|improve this question

























      0












      0








      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










      share|improve this question














      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







      subfiles includegraphics






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 28 at 5:45









      ThomasblindhardtThomasblindhardt

      102




      102






















          2 Answers
          2






          active

          oldest

          votes


















          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 {mainimages/}



          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:familydocsblindhardtimages/}{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:roadtonowhere: " 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






          share|improve this answer





















          • 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
          newcommandnewStack[1]{globallet#1@empty}
          % add element to stack (top): addStack{<stack>}{<element>}
          newcommand{addStack}[2]{defele@ment{{#2} }xdef#1{ele@ment #1}}
          % add element to stack (bottom) revaddStack{<stack>}{<element>}
          newcommand{revaddStack}[2]{defele@ment{{#2} }xdef#1{#1 ele@ment}}
          % remove from stack
          longdefpop@#1 #2@nil#3{defpopedStackElement{#1}%
          def#3{#2}}% remaining list
          % stack name #1
          newcommandpopStack[1]{letpopedStackElement@empty%
          ifx #1@emptymessage{LaTeX warning: you are trying to remove an element from the empty stack noexpand#1 on input line theinputlineno}elseexpandafterpop@#1@nil#1fi}
          % whipe stack
          newcommandcleanStack[1]{globallet#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}{currfiledirgraphicsfolder}}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}{xdefGrapPa@temp{{GrapPa@user GrapPa@auto}}expandaftergraphicspathGrapPa@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: showGrapPa@userspace - auto: showGrapPa@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.






          share|improve this answer

























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477092%2fhow-to-includegraphics-from-sub-directory-of-main-image-folder-using-subfiles%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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 {mainimages/}



            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:familydocsblindhardtimages/}{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:roadtonowhere: " 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






            share|improve this answer





















            • 1





              Your stupid-simple analogy did the trick :)!

              – Thomasblindhardt
              Mar 3 at 8:40
















            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 {mainimages/}



            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:familydocsblindhardtimages/}{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:roadtonowhere: " 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






            share|improve this answer





















            • 1





              Your stupid-simple analogy did the trick :)!

              – Thomasblindhardt
              Mar 3 at 8:40














            0












            0








            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 {mainimages/}



            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:familydocsblindhardtimages/}{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:roadtonowhere: " 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






            share|improve this answer















            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 {mainimages/}



            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:familydocsblindhardtimages/}{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:roadtonowhere: " 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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 2 at 1:45

























            answered Mar 1 at 17:24









            KJOKJO

            2,7591119




            2,7591119








            • 1





              Your stupid-simple analogy did the trick :)!

              – Thomasblindhardt
              Mar 3 at 8:40














            • 1





              Your stupid-simple analogy did the trick :)!

              – Thomasblindhardt
              Mar 3 at 8:40








            1




            1





            Your stupid-simple analogy did the trick :)!

            – Thomasblindhardt
            Mar 3 at 8:40





            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
            newcommandnewStack[1]{globallet#1@empty}
            % add element to stack (top): addStack{<stack>}{<element>}
            newcommand{addStack}[2]{defele@ment{{#2} }xdef#1{ele@ment #1}}
            % add element to stack (bottom) revaddStack{<stack>}{<element>}
            newcommand{revaddStack}[2]{defele@ment{{#2} }xdef#1{#1 ele@ment}}
            % remove from stack
            longdefpop@#1 #2@nil#3{defpopedStackElement{#1}%
            def#3{#2}}% remaining list
            % stack name #1
            newcommandpopStack[1]{letpopedStackElement@empty%
            ifx #1@emptymessage{LaTeX warning: you are trying to remove an element from the empty stack noexpand#1 on input line theinputlineno}elseexpandafterpop@#1@nil#1fi}
            % whipe stack
            newcommandcleanStack[1]{globallet#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}{currfiledirgraphicsfolder}}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}{xdefGrapPa@temp{{GrapPa@user GrapPa@auto}}expandaftergraphicspathGrapPa@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: showGrapPa@userspace - auto: showGrapPa@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.






            share|improve this answer






























              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
              newcommandnewStack[1]{globallet#1@empty}
              % add element to stack (top): addStack{<stack>}{<element>}
              newcommand{addStack}[2]{defele@ment{{#2} }xdef#1{ele@ment #1}}
              % add element to stack (bottom) revaddStack{<stack>}{<element>}
              newcommand{revaddStack}[2]{defele@ment{{#2} }xdef#1{#1 ele@ment}}
              % remove from stack
              longdefpop@#1 #2@nil#3{defpopedStackElement{#1}%
              def#3{#2}}% remaining list
              % stack name #1
              newcommandpopStack[1]{letpopedStackElement@empty%
              ifx #1@emptymessage{LaTeX warning: you are trying to remove an element from the empty stack noexpand#1 on input line theinputlineno}elseexpandafterpop@#1@nil#1fi}
              % whipe stack
              newcommandcleanStack[1]{globallet#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}{currfiledirgraphicsfolder}}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}{xdefGrapPa@temp{{GrapPa@user GrapPa@auto}}expandaftergraphicspathGrapPa@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: showGrapPa@userspace - auto: showGrapPa@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.






              share|improve this answer




























                1












                1








                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
                newcommandnewStack[1]{globallet#1@empty}
                % add element to stack (top): addStack{<stack>}{<element>}
                newcommand{addStack}[2]{defele@ment{{#2} }xdef#1{ele@ment #1}}
                % add element to stack (bottom) revaddStack{<stack>}{<element>}
                newcommand{revaddStack}[2]{defele@ment{{#2} }xdef#1{#1 ele@ment}}
                % remove from stack
                longdefpop@#1 #2@nil#3{defpopedStackElement{#1}%
                def#3{#2}}% remaining list
                % stack name #1
                newcommandpopStack[1]{letpopedStackElement@empty%
                ifx #1@emptymessage{LaTeX warning: you are trying to remove an element from the empty stack noexpand#1 on input line theinputlineno}elseexpandafterpop@#1@nil#1fi}
                % whipe stack
                newcommandcleanStack[1]{globallet#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}{currfiledirgraphicsfolder}}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}{xdefGrapPa@temp{{GrapPa@user GrapPa@auto}}expandaftergraphicspathGrapPa@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: showGrapPa@userspace - auto: showGrapPa@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.






                share|improve this answer















                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
                newcommandnewStack[1]{globallet#1@empty}
                % add element to stack (top): addStack{<stack>}{<element>}
                newcommand{addStack}[2]{defele@ment{{#2} }xdef#1{ele@ment #1}}
                % add element to stack (bottom) revaddStack{<stack>}{<element>}
                newcommand{revaddStack}[2]{defele@ment{{#2} }xdef#1{#1 ele@ment}}
                % remove from stack
                longdefpop@#1 #2@nil#3{defpopedStackElement{#1}%
                def#3{#2}}% remaining list
                % stack name #1
                newcommandpopStack[1]{letpopedStackElement@empty%
                ifx #1@emptymessage{LaTeX warning: you are trying to remove an element from the empty stack noexpand#1 on input line theinputlineno}elseexpandafterpop@#1@nil#1fi}
                % whipe stack
                newcommandcleanStack[1]{globallet#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}{currfiledirgraphicsfolder}}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}{xdefGrapPa@temp{{GrapPa@user GrapPa@auto}}expandaftergraphicspathGrapPa@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: showGrapPa@userspace - auto: showGrapPa@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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 28 at 16:18

























                answered Feb 28 at 10:50









                AndiWAndiW

                3221211




                3221211






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f477092%2fhow-to-includegraphics-from-sub-directory-of-main-image-folder-using-subfiles%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?