0

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 tellLaTeX 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?

  • Are there multiple image files called baz spread across these directories? Would you be willing to rename foo/1 to be foo/foo_1? – StrongBad Feb 10 '16 at 15:35
  • 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. – Jeffrey Lebowski Feb 10 '16 at 16:10
  • 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. – StrongBad Feb 10 '16 at 16:13
  • 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. – Jeffrey Lebowski Feb 10 '16 at 16:31
1

You can use

\graphicspath{{foo}}\input{foo/1}
..
\graphicspath{{bar}}\input{bar/1}
  • 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 – Jeffrey Lebowski Feb 10 '16 at 16:13
  • @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 – David Carlisle Feb 10 '16 at 17:13
0

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.

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.