[Tex/LaTex] dynamic paths in latex

folderspathspreamble

Im trying to link from my preamble to some pictures used in the header.
My problem is i use the same preamble in multiple documents.

lets say the preamble and the needed picture is in the top folder, and i have two different main files for two different reports, each in its own subfolder.

when i call the preamble i get an error telling me the path to the picture is not correct. as far as i have gotten it seems that i need to have two preambles with the path from the include folder to the preamble picture. (which sort of takes the idea of a preamble away)

folder structure:

work
  preamble
    pics
  project1
    main.tex
  project2
    main.tex

i would expect that in my preamble i could do this

\includegraphics[scale=0.5]{pics/image.png}

the i have tried with setting this command ind the main.tex

\newcommand{\folder}{/path/to/pic}
\includegraphics[scale=0.5]{\folder/pics/image.png}

but that doesnt work either.

does any one know how to solve this problem? (i Hope i made the problem clear)

Best Answer

You could define the search path using \graphicspath

I suppose that you could write someting like that in your preamble:

\graphicspath{{./}{/path/to/pic/}}

\includegraphics will search first in the current directory (./), then in /path/to/pic

So that your picture could be used as:

\includegraphics[scale=.5]{pics/image}

The image.png will be first searched as ./pics/image.png, and if not found as /path/to/pic/pics/image.png

Hints: you should not add the '.png': write image instead image.png


Other possibility:

You have to transform your "preamble" into a TeX package by moving all your preamble folder in your personal TEXMF tree (or make symbolic link, if you are working on linux) or make your TeX distribution search in your preamble folder.


Third option:

On smart OS, you could make symbolic links to point to the preamble folder, and use relative path to access to your pictures. If you move your main folder, you just have to adapt this links.

On linux, a symbolic link named "folder" is created like this:

ln -s /path/to/target/folder folder