Title: input and graphicx - look for images where file is located
Tags: graphics;input;directory
Question: I have a LaTeX document split in multiple text files, inside various folders, as follows:

```\section{Foo}

\subsection{Foo1}
\input{foo/1}

\subsection{Foo2}    
\input{foo/2}

\section{Bar}

\subsection{Bar1}
\input{bar/1}
```

And so on. Suppose that, inside the ```Foo2``` subsection, I must insert an image. How can I tell```LaTeX``` to look for the image inside the directory the currently open file is (i.e. ```foo```) so that inside the file ```2``` I can just write:

```\includegraphics{baz}
```

and I can place all images for a determined section inside their folders, among that section's input text files?

Comment: I have multiple graphics file inside each directory, named differently. My actual file names are a little explicative, not just numbers like used in this example. Something around `theory/plasma` `theory/probes` and so on.

Comment: Better than my original question, is there a declaration like `\graphicspath` which functions both for images and text files? This way, I could set it at the beginning of each section, and input subsections and images with just their names, without having to repeat the relative path for every one of them.

Comment: Are there multiple image files called `baz` spread across these directories? Would you be willing to rename `foo/1` to be `foo/foo_1`?

Comment: I think if you use the TEXINPUTS environment variable you will be fine as long as there are no duplicate `tex` filenames. Similarly, you can set `\graphicspath` once if there are no duplicate image filenames.


Here is another answer: There are a number of ways of tackling this depending on how your directories are laid out. If you directory tree looks like

```-foo
  -foo1.tex
  -foo2.tex
  -baz.pdf
  -baz.eps
-bar
  -bar1.tex
  -qux.pdf
```

where the files in the ```foo``` and ```bar``` directories have unique names, then you can specify the ```TEXINPUT``` environment variable to include the sub-directories. The details of how to set ```TEXINPUTS``` depend on your distribution (e.g., TeXLive or MikTeX) and OS (e.g., Windows or Linux). In my opinion, this is the ideal setup since you are telling ```tex``` where to look for files. In this case your "main" file would look like

```\section{Foo}
\subsection{Foo1}
\input{foo1}
\subsection{Foo2}    
\input{foo2}
\section{Bar}
\subsection{Bar1}
\input{bar1}
```

You could also use ```\graphicspath``` in this situation to set the directories to look for images in. In this case, the main file would look like

```\graphicspath{{foo}{bar}}
\section{Foo}
\subsection{Foo1}
\input{foo/foo1}
\subsection{Foo2}    
\input{foo/foo2}
\section{Bar}
\subsection{Bar1}
\input{bar/bar1}
```

If you directory tree looks like

```-foo
  -1.tex
  -2.tex
  -baz.pdf
  -baz.eps
-bar
  -1.tex
  -qux.pdf
  -baz.pdf
  -baz.eps
```

where the files in the ```foo``` and ```bar``` directories do not have unique names, then you cannot specify the ```TEXINPUT``` environment variable since it will not know which file you mean. You can still use ```\graphicspath```, but you have to set it before each ```\input```

```\graphicspath{{foo}}
\section{Foo}
\subsection{Foo1}
\input{foo/1}
\subsection{Foo2}    
\input{foo/2}
\graphicspath{{bar}}
\section{Bar}
\subsection{Bar1}
\input{bar/1}
```

You could also use the ```import``` package.

```\section{Foo}
\subsection{Foo1}
\import{foo/}{1}
\subsection{Foo2}    
\import{foo/}{2}
\graphicspath{{bar}}
\section{Bar}
\subsection{Bar1}
\import{bar/}{1}
```

Things get a little tricky with the ```import``` package if your directories are nested.


Here is another answer: You can use

```\graphicspath{{foo}}\input{foo/1}
..
\graphicspath{{bar}}\input{bar/1}
```

Comment for this answer: Isn't there a way to tell `LaTeX` that graphics should be picked inside the directory I'm in? I mean, it's obviously gonna look for images inside the directory the main `.tex` file is (or, actually, the folder I'm compiling in, I suppose). Instead, I want it to look for them inside the directory the input file is. I'm writing inside it, I want it to take its own directory for `\includegraphics`
Comment for this answer: @JeffreyLebowski `\input` is designed to work like cutting and pasting the file contents in to that point tex really does not expose the current location of the file. there are packages that offer wrappers that  offer slightly different interface but they will basically be doing exactly the above

{'source': 'StackExchangeDataset', 'subset': 'tex.stackexchange.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:02/10/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<graphics><input><directory>', 'Title': 'input and graphicx - look for images where file is located', 'ViewCount': 450, 'id': 292166, 'user_AboutMe': '<p>The Dude minds.</p>\n', 'user_AccountId': 3728694.0, 'user_DisplayName': 'Jeffrey Lebowski', 'user_Reputation': 511.0, 'user_Views': 107.0, 'user_id': 42723.0}}