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/.

bill999
  • 463
  • 3
    Use \input{../../Files/myfile.tex} – Dox Jul 21 '14 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 Jul 21 '14 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 Jul 21 '14 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 Jul 21 '14 at 15:39

2 Answers2

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.

  • I can call this approach as my best practice but you may call it as a bombastic approach. :-) – kiss my armpit Jul 21 '14 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 Jul 21 '14 at 16:17
  • 1
    @bill999: Please first make sure your path does not contain any spaces. – kiss my armpit Jul 21 '14 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 Jul 21 '14 at 16:21
  • @bill999: You can enclose the path with double quotes, e.g., "<your path>". But having spaces invites unnecessary problems. – kiss my armpit Jul 21 '14 at 16:27
  • @bill999: Is your file to input self-contained or compilable? If yes, then you need to load docmute package in your main.tex to extract the body of the file to input. – kiss my armpit Jul 21 '14 at 16:35
  • I tried the quotes and they didn't help. They are the " quotes and not the `` '' quotes, correct? Note that my filename does not contain spaces - only the folder names.
  • – bill999 Jul 21 '14 at 16:38
  • I can successfully use my additional .tex file if it is loaded into the same folder as main.tex. So I don't think docmute is necessary.
  • – bill999 Jul 21 '14 at 16:39
  • 1
    @bill999: OK. Then you must not use spaces in folder name to remove complexity. I will update my answer about the quotes later. – kiss my armpit Jul 21 '14 at 16:40
  • Nothing would work. It's not a good idea to change my files at this point because other things are hard coded to use them. Anyway, I was able to just use relative file paths without needing to use input. This was simple and it works perfectly. Thanks for all of the help. In the future, knowing about the problem with the spaces in file names and folders should prove to be beneficial. – bill999 Jul 21 '14 at 16:57
  • @bill999: How do you import a file from with an input file with relative path and without input? – kiss my armpit Jul 21 '14 at 17:03
  • You're right - I do still need to use input. See the longer clarification in my answer. – bill999 Jul 21 '14 at 17:11