9

I have a simple question. How do I access files outside of my current directory in LaTeX? I have a project that I am updating and saving each day's version in a separate folder. I would like to have a different folder dedicated to hold my auxiliary `.tex' file, graphics, and bibliography file. Then be able to access these files from my current folder. I am using a Mac if that makes any difference.

My folder structure is as follows:

(for main .tex file): .../Project/7-21

(for additional files): .../Project/Files/

So my main file is stored in /7-21/ and the files I want to access are in /Files/.

4
  • 3
    Use \input{../../Files/myfile.tex}
    – Dox
    Commented Jul 21, 2014 at 15:12
  • Thanks for the suggestion. Unfortunately, it says that the file cannot be found. I got the file path from 'Get Info' after right-clicking on my file.
    – bill999
    Commented Jul 21, 2014 at 15:16
  • 2
    Dox is correct. So either the file isn't there or you mistyped the path or filename. Please bear in mind that path and file names are case sensitive and be careful about the use of spaces in file names.
    – JPi
    Commented Jul 21, 2014 at 15:23
  • 2
    While Dox's solution should happen to work, it is not recommended. Ideally you fiddle with TEXINPUTS. However, you may also find the import package of value.
    – jon
    Commented Jul 21, 2014 at 15:39

2 Answers 2

7

Assume that your directory structure is as follows.

<any folder ignored for the sake of simplicity>/MyFiles/Images/diagram-01.tex
<any folder ignored for the sake of simplicity>/MyFiles/Images/diagram-02.tex
<any folder ignored for the sake of simplicity>/MyFiles/Images/diagram-03.tex
<any folder ignored for the sake of simplicity>/MyFiles/Projects/Report-01/main.tex
<any folder ignored for the sake of simplicity>/MyFiles/Projects/Report-02/main.tex
<any folder ignored for the sake of simplicity>/MyFiles/Projects/Article-01/main.tex

Images contains diagrams that will be shared among many reports. Each report or article are saved in a separate folder to ease maintenance.

So you can, for example, import the diagram-01.tex from the first report's main.tex as follows.

% my first report
% main.tex
\documentclass{report}

\begin{document}
\input{../../Images/diagram-01.tex}
\end{document}

Or if the diagram has been converted to PDF format, you can import it as an image as follows.

% my first report
% main.tex
\documentclass{report}
\usepackage{graphicx}
\graphicspath{{../../Images/}}
\begin{document}
\includegraphics{diagram-01}
\end{document}

Here \graphicspath declares the path globally. By the way, \graphicspath can also be invoked as follows

\graphicspath{{../../Images/}{<any path>/}{<any path>/}{<any path>/}<...>}

where <...> represents that you can add more and more paths, but don't include <...> for sure.

Notes

If the file, i.e., diagram-01.tex that you want to import from within the main.tex is self-contained input file which can be compiled then you need to load docmute package in the main.tex such that main.tex will just import the contents sandwiched between \begin{document} and \end{document} of the diagram-01.tex.

12
  • I can call this approach as my best practice but you may call it as a bombastic approach. :-) Commented Jul 21, 2014 at 15:36
  • I am getting some weird stuff happening. When I try to access a .tex file using input, it doesn't work. (I am actually using another function and putting input inside of this function.) When I try accessing a graphic using includegraphics+input, it doesn't work. When I use includegraphics and just put in the file path, it inserts the graphic, but also prints out the file path onto the screen. Does using graphicspath apply only to this main .tex file or to every other .tex file as well? I am not sure about how a global variable is defined in LaTeX.
    – bill999
    Commented Jul 21, 2014 at 16:17
  • 1
    @bill999: Please first make sure your path does not contain any spaces. Commented Jul 21, 2014 at 16:19
  • Good point. I looked and it does, in fact, have spaces. Is there anything to be done other than actually changing the folder and/or file names to remove the spaces?
    – bill999
    Commented Jul 21, 2014 at 16:21
  • 1
    @bill999: OK. Then you must not use spaces in folder name to remove complexity. I will update my answer about the quotes later. Commented Jul 21, 2014 at 16:40
1

The thing that ultimately worked for me was to simply use relative file paths.

So, using the file path in my original answer, to access tab1.tex in /files, I would just do:

../files/tab1.tex

(This works with input and includegraphics.

Also, as is seen elsewhere in this question, how to use absolute file paths where folders contain spaces is still unanswered (although one could just remove the spaces in the actual file paths). But this is unnecessary for my application as the relative file paths does just fine.

4
  • I don't understand how you can import an external file without \input. It looks like a magic. Commented Jul 21, 2014 at 17:05
  • Regardless of relative paths or absolute paths, you need to enclose the paths containing spaces. Commented Jul 21, 2014 at 17:15
  • I was wrong - I still do need to include input. I misunderstood something. I am actually a different function (other than input) to access my .tex file. I was trying to nest input inside of this other function, which is the wrong thing to do. Anyway, using the relative file position works with input and includegraphics. Sorry for the confusion and thanks for pointing my error out to me. I am new at this kind of stuff and this is helping me gain a better understanding of what is going on.
    – bill999
    Commented Jul 21, 2014 at 17:16
  • Luckily my relative path contains no spaces.
    – bill999
    Commented Jul 21, 2014 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .